Servlet generates PDF but sometimes the invocation behaviour gets bogged down
A servlet generates a PDF document after a preview page has been generated within an iframe which is generated at first. This usually works and looks like this:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if ("directPrintDocumentDoIt".equals(request.getParameter("method"))) {
generatePDF(request, response);
}
if ("directPrintDocumentWaiting".equals(request.getParameter("method"))) {
String queryString = request.getQueryString().replace("directPrintDocumentWaiting", "directPrintDocumentDoIt");
renderWaiting(request, response, queryString);
}
if ("directPrintDocument".equals(request.getParameter("method"))) {
String queryString = request.getQueryString().replace("directPrintDocument", "directPrintDocumentWaiting");
renderIFrameWaiting(request, response, queryString);
}
}
First of all "directPrintDocument" and the method "renderIFrameWaiting" is invoked which generates the iframe page and writes the content to the servlet response (The iframe src invokes the next part of doPost. I'm sure this produces no errors so I omit this code fragment). Then "directPrintDocumentWaiting" and the method "renderWaiting" is invoked which generates a preview page within the previous generated iframe page while the generated javascript invokes "directPrintDocumentDoIt" which finally generates the PDF:
private void renderWaiting(HttpServletRequest request, HttpServletResponse response, String queryString) throws IOException {
StringBuffer output = new StringBuffer();
response.setContentType("text/html; charset=ISO-8859-1");
response.setHeader("Cache-Control", "no-cache");
output.append("<html>\n");
output.append("<head>\n");
output.append("<meta http-equiv='Content-Type' content='text/html;charset=iso-8859-1'>\n");
output.append("<meta http-equiv='expires' content='0'>\n");
output.append("<meta http-equiv='cache-control' content='no-cache'>\n");
output.append("<meta http-equiv='pragma' content='no-cache'>\n");
output.append("</head>\n");
output.append("<script type=\"text/javascript\">\n");
output.append("function formSubmit() {\n");
output.append("document.forms[0].target=\'_self\';\n");
output.append("document.body.style.cursor = \"wait\";\n");
output.append("var formAction = document.forms[0].action;\n");
output.append("document.forms[0].submit();\n");
output.append("}\n");
output.append("</script>\n");
output.append("<body onload=\"self.focus(); formSubmit();\">\n");
output.append("<form name=\"druckenForm\" method=\"post\" action=\"" + request.getRequestURI() + "?" + queryString + "\" onsubmit=\"return false;\">\n");
output.append("<p align=\"center\" valign=\"center\">Druck wird erzeugt...\n</p>\n");
output.append("<p align=\"center\" valign=\"center\">Der erstmalige Start kann etwas länger dauern.</p>\n");
output.append("</form>\n");
output.append("</body>\n");
output.append("</html>");
response.setContentLength(output.length());
response.getOutputStream().write(output.toString().getBytes());
response.getOutputStream().flush();
response.getOutputStream().close();
}
This usually works but sometimes something strange happens. Somehow it looks like that the last invocation "directPrintDocumentDoIt" will be invoked twice so generatePDF will be invoked twice too and the whole printout gets bogged down (in the most cases the PDF will be overridden by a white page). This happens by a chance of 1 of 50 times, so it's hardly ever reproducable. First of all I thought of some thread issues so I logged the thread id at every invocation:
log.info("> current thread:" + Thread.currentThread().getId());
The thread id is always the same, so I doubt it's really a thread problem. What perplexes me is the fact that when this error occures fiddler records a 4th invocation after the javascript POST invocation has been executed. Usually there are 3 invocations ("directPrintDocument", "directPrintDocumentWaiting" and "directPrintDocumentDoIt"). When there are 4 invocations it always happens in the same manner: "directPrintDocumentDoIt" will be invoked twice (same URL with the POST request) but suddendly as a GET request. I have no clue where 开发者_开发百科this GET request comes from (In "generatePDF" are no other request invocations). So either the javascript produces some strange behaviour (but why then very seldom) or the web container (Websphere) makes some strange things I don't understand. The interesting thing is that fiddler shows different entity sizes at the 3rd and 4th invocation. The POST invocation which is the right one has an entity size of 84.138 bytes. The 4th invocation (the wrong one) has 83.883 bytes. I don't know what this means but maybe this information is helpful. I have the feeling that this errors happens more often after I restarted or published to Websphere. In this case it happens at the first try (but not always). This cannot be coincidence. Any ideas what's going on here?
Obviously the error was caused by the pdfwriter. The errorprone code looked like this:
response.reset();
response.setContentType("application/pdf");
response.setContentLength(pdfContent.length);
response.setHeader("Content-Disposition", "inline; filename=RANDOMFILE.pdf");
response.addHeader("Accept-Ranges", "bytes");
response.getOutputStream().write(pdfContent);
response.getOutputStream().flush();
response.getOutputStream().close();
I don't know why, but sometimes this header causes anyhow a 2nd request. When I change it to the following, I get no white pages anymore after pdf generation. So this seems to be the solution.
response.setContentType("application/pdf");
response.setContentLength(pdfContent.length);
response.getOutputStream().write(pdfContent);
response.getOutputStream().flush();
response.getOutputStream().close();
精彩评论