Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6614141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:18:09+00:00 2026-05-25T20:18:09+00:00

A servlet generates a PDF document after a preview page has been generated within

  • 0

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&auml;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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:18:10+00:00Added an answer on May 25, 2026 at 8:18 pm

    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(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
A user has a few Lookup selections on a JSP page. After he's selected
I have a cookie which is generated from a servlet and that I would
I have a J2EE webapp which is used to download a generated PDF served
My servlet application includes a number of library .jars, some of which contain embedded
I have a servlet that I would like to run within ColdFusion MX 7.
Is there any way with which I can capture generated dynamic content on the
I have a servlet in which I want to output normal text and also
I have a Java servlet container using the Spring Framework. Pages are generated from
I have used maven plugin for hibernate and it has generated entity classes without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.