IllegalStateException: getWriter() has already ben called for this response

java.lang.IllegalStateException: getWriter() has already been called for this response

at org.apache.coyote.tomcat4.CoyoteResponse.getOutputStream(CoyoteResponse.java:572)

at org.apache.coyote.tomcat4.CoyoteResponseFacade.getOutputStream(CoyoteResponseFacade.java:156)

at com.arrow.dpa.controller.ExcelDownload.downloadFile(ExcelDownload.java:91)

at com.arrow.dpa.controller.ExcelDownload.downloadFile(ExcelDownload.java:55)

at org.apache.jsp.Purchase_ExcelDownload_jsp._jspService(Purchase_ExcelDownload_jsp.java:301)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)

at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)

at com.arrow.dpa.controller.DPAPurchaseMaintenanceAction.run(DPAPurchaseMaintenanceAction.java:295)

at com.arrow.dpa.controller.ControllerServlet.doPost(ControllerServlet.java:135)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:716)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)

at java.lang.Thread.run(Thread.java:534)

[4344 byte] By [ShanmugaPriyaa] at [2007-10-3 4:53:15]
# 1
*sigh*[url= http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]
TimTheEnchantora at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for the stack trace. It has been filed appropriately.
warnerjaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 3
I am able to resolve the exception.Can anyone plz explain me the root cause and provide solution.
ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 4
> I am able to resolve the exception.Hai Shanmuga,Please call getWriter once only in your response. this may be source of prob otherwise.
cotton.ma at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks :)

Code that i have written is given below.. Plz find the prob..

response.setContentLength((int)fileLen);

//response.setHeader("Content-Disposition", "attachment; filename=".concat(String.valueOf(file.getName())));

response.flushBuffer();

response.setContentType("application/x-msdownload");

excel.downloadFile(pathurl+"Sales_Maintenance.csv","application/x-msdownload","rptinexcel.csv");

boolean deleted=false;

if (file.exists())

deleted= file.delete();

response.flushBuffer();

response.reset();

pageContext.forward("Sales_Maintenance.csv");

ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 6

> pageContext.forward("Sales_Maintenance.csv");

This is the error right here.

When you forward that sets another header BUT you cannot output any headers AFTER you have output content.

Try getting rid of the response.flushBuffer() line. That's probably sending content and preventing you from forwarding.

cotton.ma at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 7
Actually i am using response object already in another jsp page for the same purpose and same code also...Do u think the problem is bcoz of that..If you think so then what should i do to resolve the problem..Plz help.
ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 8
Please try and comment out all the response.flushBuffer() first and see what happens.Your other problem may be a problem but it's hard to tell what is going on.So let's try one thing at a time.
cotton.ma at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 9

> Actually i am using response object already in

> another jsp page for the same purpose and same code

> also...

> Do u think the problem is bcoz of that..

>

> If you think so then what should i do to resolve the

> problem..

> Plz help.

A JSP document, which runs as a servlet, always do implicit response output handling code written in the document, i.e. getWriter() is already implicitly called. I always teach "keep your jsp as simple as possible, single jsp should do a single job". If you do a heavy job via single jsp, you are very likely to get a 'IllegalStateException: getWriter() has already ben called for this response' exception. That is one of the most frequent errors in JSP programming.

hiwaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 10
I tried by commenting response.flushbuffer(); but still no solution...what to do?
ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 11
On to the next step. Review hiwa's post.
cotton.ma at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 12
You can't reuse response.Read my previous reply and abide by the KISS principle.A JSP that does a complex job is a sign of bad desgin.
hiwaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 13
Ya I read that.. I think he really understood the true picture of the problem..if getWriter() is implicitly called then how can we able to do output handling again in the other forthcoming jsp pages..Plz give the solution to overcome
ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 14
Thanks Hiwa :)Ya i understood ur explanation..If we cant reuse response then how can we implement the above given logic(plz refer code) and do output handling in the forthcoming pages.Plz help me with the sample code..plz..
ShanmugaPriyaa at 2007-7-14 22:58:06 > top of Java-index,Java Essentials,Java Programming...
# 15
KISSKeep It Simple Shanmuga!
cotton.ma at 2007-7-21 10:46:34 > top of Java-index,Java Essentials,Java Programming...
# 16
plz give me code solution to how to reuse the response object
ShanmugaPriyaa at 2007-7-21 10:46:34 > top of Java-index,Java Essentials,Java Programming...
# 17

> plz give me code solution to how to reuse the

> response object

You must recognize that it is not a coding issue but a fundamental design issue.

And we can't touch your design level problem here by casual interaction on a public forum.

Dissect you whole application requirement into a most appropriate subtasks hierarchy, graph, or other structure comprising from servlets, JSPs and other supporting objects. And keep each JSP to do just one single task only -- don't explicitly call output in it nor reuse response.

Above is the most friendly advice from a forum friend who does not have ample spare time.

hiwaa at 2007-7-21 10:46:34 > top of Java-index,Java Essentials,Java Programming...
# 18

> Thanks Hiwa :)

> Ya i understood ur explanation..

>

> If we cant reuse response then how can we implement

> the above given logic(plz refer code) and do output

> handling in the forthcoming pages.

>

> Plz help me with the sample code..

> plz..

Do all the loading of data in the first page, store it in the session, i.e. in a bean, forward to next page, read from the session.

IMHO, you should use a pure servlet and not a jsp-page to do file-access and stuff like that otherwise you tend to try to do output when no outout should be done.

Lajma at 2007-7-21 10:46:34 > top of Java-index,Java Essentials,Java Programming...
# 19

> > Thanks Hiwa :)

> > Ya i understood ur explanation..

> >

> > If we cant reuse response then how can we

> implement

> > the above given logic(plz refer code) and do

> output

> > handling in the forthcoming pages.

> >

> > Plz help me with the sample code..

> > plz..

>

> Do all the loading of data in the first page, store

> it in the session, i.e. in a bean, forward to next

> page, read from the session.

>

> IMHO, you should use a pure servlet and not a

> jsp-page to do file-access and stuff like that

> otherwise you tend to try to do output when no outout

> should be done.

That's right. To do forwarding, or other complex logic for that matter, on a JSP is dangerous because, in general or typically, JSP is destined to do some response output implicitly.

hiwaa at 2007-7-21 10:46:34 > top of Java-index,Java Essentials,Java Programming...