This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes response.sendRedirect giving illegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "response.sendRedirect giving illegalStateException" Watch "response.sendRedirect giving illegalStateException" New topic
Author

response.sendRedirect giving illegalStateException

Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Hi,

I am trying to learn JSP. I am trying to code a simple page with a form submission. The code is:





imageUploader.jsp




The code is really simple but I dont understand why I am getting the error:

java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at org.apache.jsp.imageUploader_jsp._jspService(imageUploader_jsp.java:139)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)




Please help me out here...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

from sendRedirect Javadoc :

If the response has already been committed, this method throws
an IllegalStateException.
After using this method, the response should be considered
to be committed and should not be written to

you are including a jsp to show to user, immediately redirecting to other page which defeat the purpose of include action.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Specifically the flush="true" attribute is flushing the buffer, and committing the response.

You either need to
- remove the flush="true" attribute
- put the response.redirect earlier in the code

Another thing you may need to know.
Just because you TOLD the server to redirect the response doesn't mean the current page stops running
After issuing a forward/redirect, you should immediately return from your page.
Standard approaches:


or


Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Stefan Evans wrote:Specifically the flush="true" attribute is flushing the buffer, and committing the response.

You either need to
- remove the flush="true" attribute
- put the response.redirect earlier in the code

Another thing you may need to know.
Just because you TOLD the server to redirect the response doesn't mean the current page stops running
After issuing a forward/redirect, you should immediately return from your page.
Standard approaches:


or




Thank you....You have solved the problem..:)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: response.sendRedirect giving illegalStateException