File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes getOutputStream() has already been called for this response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "getOutputStream() has already been called for this response" Watch "getOutputStream() has already been called for this response" New topic
Author

getOutputStream() has already been called for this response

Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 108
Hi,

I am getting the folllowing exception when I try to generate a pdf


java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:122)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:237)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:173)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:124)
at org.apache.jsp.pages.common.GeneratePdf_jsp._jspService(org.apache.jsp.pages.common.GeneratePdf_jsp:256)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

My code is:



I googled and found no solution.

Some one is saying to return null. some one is saying call the action through another jsp, another is saying call the servlet directly.

Actually I didnt understand what exactly is that. Action class is nothing but a Servlet. I tried returning null also. I used another jsp to call action but it didnt avoid the exception. I dont know where I am going wrong.

Please guide me how to avoid it. As I have to return some success message also after generating the pdf so I cant return null. With the above code I am able to gnerate the pdf but not able to get rid that exception and aslo I am not able to display the success msg in the jsp.

Thanking you.

-- raaja. g
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Hi Raaja,
Please close your servlet output stream, which would solve your problem. and close it finally
Caution:: Never catch a generic exception as above,


Have Fun with Java
little,little.. little by little makes a lot..
Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 108
Hi,

The problem still persists. If I set return null in the action the exception is not coming. And also another problem came as it is a web application If I access from client machine and try to generate pdf its giving message format error:not a PDF or corrupted. If the same application deployed and run from local machine (os: winxp) it perfectly generates the pdf.

Where I am going wrong. Please correct me.

Thanking you.

--raaja. g
Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 108
Hi,

I am getting the java.lang.IllegalStateException: getOutputStream() has already been called for this response when I use return map.findForward("success"); at the end of action class. But this exception wont appear if I return null. But this will not display an error message in the jsp through action messages.

How to avoid the above said exception so that I can generate a pdf when success and print a message when failed.

My code is:

Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

Why would you return "success" at any cost? Why don't you use appropriate return statements for failure of PDF generation and other cases? In case your PDF generation was successful, returning null doesn't cost you a fortune, does it? I stress this because,

Oh wait, it's rather worth quoting this:

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.


This suggests you're trying to forward to a JSP after your action completes, which won't work since you've already sent a PDF back to the browser. Your execute() method should be returning 'null' to tell Struts not to do this.


Coderanch, best ever forum on the net. Hands down.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getOutputStream() has already been called for this response
 
Similar Threads
javax.servlet.ServletException: Skipping validation rules file from '/WEB-INF/validator-rules.xml'.
java.lang.IllegalArgumentException: The path of an ForwardConfig cannot be null
Web app IllegalStateException: getOutputStream()..
Need to send message to struts JSP after creating PDF
Class Not Found Exception