• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • 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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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