[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

getOutputStream() repeats servlet call


Outputting to pdf from a servlet results in the servlet call being repeated.  I'm using the Apache fop.jar file to render the pdf.  Debugging shows that the getOutputStream() line below results in a repeated call to the servlet.  The screen renders correctly but the log shows that the servlet call executed twice.  

attr.getResponse().setContentType("application/pdf");
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
....
byte[] content = byteOut.toByteArray();
attr.getResponse().setContentLength(content.length);
attr.getResponse().getOutputStream().write(content);
attr.getResponse().getOutputStream().flush();

Does anyone know what would cause a repeat call to the servlet?

Thx.

jkabx
0
jkavx
Asked:
jkavx
  • 11
  • 9
1 Solution
 
objectsCommented:
Repeated call to which servlet?
0
 
jkavxAuthor Commented:

The same servlet.  A call to ABC renders correctly but produces a second call to ABC.
0
 
objectsCommented:
So your saying the browser is calling the servlet twice?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
How is the servlet being called from the browser?

0
 
jkavxAuthor Commented:

The browser calls the servlet.  Pdf output is rendered correctly in Acrobat.  But the log shows that the servlet call occurred twice:

#### PdfControllerServlet started for action: <HYLoanPdfView with pdf=true>
#### PdfControllerServlet finished for action: <HYLoanPdfView>

#### PdfControllerServlet started for action: <HYLoanPdfView with pdf=true>
#### PdfControllerServlet finished for action: <HYLoanPdfView>

jkavx
0
 
objectsCommented:
How is the browser making the call exactly?
0
 
jkavxAuthor Commented:

The user clicks a link on the screen.  The link is referenced to a Url.  The Url is a registered servlet call.  This is running in Weblogic 8, sp3.
0
 
objectsCommented:
Can you post the link
0
 
jkavxAuthor Commented:

http://localhost:7001/EBK/HYLoanPdfView.pcs?projectId=76000160909

*.pcs is mapped to PdfControllerServlet

When I use this servlet for standard calls, everything is fine.  But with the pdf calls, where these lines are involved in writing the output, the repeated calls occur:

  attr.getResponse().setContentLength(content.length);
  attr.getResponse().getOutputStream().write(content);
0
 
objectsCommented:
I meant the html
0
 
jkavxAuthor Commented:

It's not html.  Html (or actually xhtml) is transformed into fo with a stylehseet and output as pdf using the Apache fop.jar.  Let me put all the java code:

attr.getResponse().setContentType("application/pdf");
Driver driver = new Driver();
Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
MessageHandler.setScreenLogger(log);
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
driver.setOutputStream(byteOut);
            
// Create xml and xsl sources and do Pdf transformation
String sXslPdfFile = EtgEnvironment.getXSLDirectory() + XHTML_TO_FO_XSL;
Source srcXslPdf = new StreamSource(new File(sXslPdfFile));
Reader reader = new StringReader(sHtml);
Source srcXmlPdf = new StreamSource(reader);
Transformer tPdf = TransformerFactory.newInstance().newTransformer(srcXslPdf);
Result res = new SAXResult(driver.getContentHandler());
tPdf.transform(srcXmlPdf, res);

// Output Pdf
byte[] content = byteOut.toByteArray();
reader.close();
attr.getResponse().setContentLength(content.length);
attr.getResponse().getOutputStream().write(content);
attr.getResponse().getOutputStream().flush();
0
 
objectsCommented:
I meant the html that is calling the servlet :)

Also what is attr in the code above.
0
 
objectsCommented:
Also what browser are you using? Have you tried a different browser?
0
 
jkavxAuthor Commented:

Sorry.  attr is just an object that holds some basic info and the HttpServletRequest/HttpServletResponse objects.  Here's the html:

<td align="left">
  <a class="headinglink" href="EBK/HYLoanPdfView.pcs?projectId={$PROJECTID}">
    Export page to PDF
  </a>
</td>
0
 
jkavxAuthor Commented:

Using IE6.  Another browser is not an option.
0
 
objectsCommented:
What about the browser, IE is known to do this sort of thing when downloading pdf's. Have you tried it with Firefox?
0
 
jkavxAuthor Commented:

This is a corporate environment.  Changing from IE is not an option.  If it's a known issue, then that answers the question.  From a user's point of view, everything is fine.  The page returns as pdf.  It just takes a little longer.  I just want to be sure I'm not doing something wrong, or if there's some way to prevent the repeat call.
0
 
objectsCommented:
> Changing from IE is not an option.

The suggestion was more as a test, not a solution.

> I just want to be sure I'm not doing something wrong, or if there's some way to prevent the repeat call.

You're not doing anything wrong that I can see.
The above links have a few things to try to stop IE making the second request.

0
 
jkavxAuthor Commented:

Thx.  The setup is too complex to try to run from home.  I'll check the links.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now