Question

I'm getting IllegalStateException from Tomcat server, when I try to save a JSP's data in EXCEL. IllegalStateException can occur in any of the following condition:

  • If there is no return statement after sendRedirect().
  • If POST size is greater than 2MB.
  • If page buffer size is less.

Other than this, is there any causes for IllegalStateException?

My code:

response.sendRedirect(response.encodeRedirectURL(request.getRequestURI() + "?saveToFile=true&action=" + ReportsManager.ACTION_SAVE_AS_XLS + "&saveFileName=" + saveFileName + "&returnPage=" + StringFunctions.encodeUrlParameter(returnPage)));    
return;

I'm getting IllegalStateException at sendRedirect().

Also if there are only few records (say 100 rows) in JSP, then there is no error in saving as Excel. If there is 1000 records, then I'm getting IllegalStateException.

Stack trace:

*Mar 25, 2013 6:57:26 AM,JSP Runtime Error,0,java.lang.IllegalStateException
       at org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:352)
       at org.apache.jsp.OperationalReports.SalespersonSales_jsp._jspService(SalespersonSales_jsp.java:2567)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
       at com.retailJava.core.filters.SecurityFilter.doFilter(SecurityFilter.java:136)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
       at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
       at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
       at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
       at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
       at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
       at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
       at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
       at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
       at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
       at java.lang.Thread.run(Thread.java:595)

If I set page buffer size, then it worlks fine for 1000 records. But if it reaches 2000 records, then again I'm getting issue.

<%@ page buffer = "2048kb" autoFlush = "true" %>
Was it helpful?

Solution

The Documentation states:

sendRedirect Throws: IllegalStateException - if this response has already been committed

Since disabling the maxPostSize does not solve the problem (according to OP comments) it seems that at an earlier point the responce is beeing commited.

You can verify this by checking the isCommited flag.

OTHER TIPS

If you call twice "response.sendRedirect()" method, or url is a wrong url,this error may cause

Encounter the same error in different use case.

enter image description here

Use Case : In Hibernate, when we try to access any method on session object after closing the session intentionally.

Session session = /*Getting session object using factory*/
tx = session.beginTransaction();
System.out.println("Flush Mode before closing session -> " +session.getFlushMode());

session.close();
System.out.println("Flush Mode after closing session -> " + session.getFlushMode());

Key Point

  • IllegalStateException occur due to method invocation at in appropriate time
  • In context of session, we can't call any method of session once it is closed
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top