• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

IllegalStateException: getOutputStream() has already been called for this response

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
On my web page (Velocity), I have a link which when clicked opens a new page/ window (jsp). This page contains code to call my method in Java which accepts a request (HttpServeltRequest) and a response(HttpServletResponse) parameters, does some logic to generate a PDF file which is present in content byte array. And then sends it back to the browser via the response object.

Code in Java Class below:

public synchronized void processRequest(HttpServletRequest request, HttpServletResponse response) {

...... some code......
.................
.................
content = renderXML(xmlContent, xslTemplate);
if (content != null) {
//Send the result back to the client
response.setContentType("application/pdf");
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
}
}

But it throws the exception �
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
----- Root Cause -----java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.coyote.tomcat4.CoyoteResponse.getWriter(CoyoteResponse.java:552)

Any idea how to get around this problem?? I require the solution immediately. I also tried the below code and got the same exception.

OutputStream os = response.getOutputStream();
os.write(content);
os.flush();

Interestingly the pdf gets displayed in the browser with both the codes. But the exception is thrown as well. Also, the original JSP Page is displayed as a blank page.. I need to close that window when the PDF is displayed.

Any help would be appreciated.
Thanks in advance.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic rule of HTTP: one request, one response.

You can only send back one thing to a request.
Either an HTML page, or a PDF document, or an image or....
Specifically you can't send an HTML page AND a PDF document.
Java complains if you have already obtained a writer/outputstream as you should only be getting ONE of these.

JSPs by default send back text, and so obtain the writer for themselves.
If you are sending back a PDF, and trying to get the output stream, obviously it raises a conflict of interest.

Solution - use a servlet rather than a JSP to call your PDF code.
 
Don't listen to Steve. Just read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic