This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Struts and the fly likes java.lang.IllegalStateException: Cannot create a session after the response has been committed Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Frameworks » Struts
Reply Bookmark "java.lang.IllegalStateException: Cannot create a session after the response has been committed" Watch "java.lang.IllegalStateException: Cannot create a session after the response has been committed" New topic
Author

java.lang.IllegalStateException: Cannot create a session after the response has been committed

Brian Braun Mitman
Greenhorn

Joined: May 26, 2010
Posts: 14
Hi,

First of all, I'm using Tomcat 6.0.29 (the latest as of September 2010), Struts and other technologies not relevant here.

My website runs fine 99% of the time, but several times per day I get an error in my Tomcat Log, so it is an sporadic issue. I don't know the reason. My code is separated in model/view/controller, given that I'm using Struts, so my code has been correctly separated in layers. This error happens in several JSP pages.
As far as I can understand, Tomcat generates a Java class (a servlet) for every JSP (well, everybody know that I guess). When my problem arrises, an exception is being thrown when the corresponding generated servlet tries to run the "_jspService()" method that uses a lot of "out.write()" methods to send the output to the response. An exception is being thrown for some reason, and then my log gets the error trace.
Anybody knows what is going on?


This is the error in the log:

Sep 21, 2010 3:18:42 PM org.apache.jasper.runtime.JspFactoryImpl internalGetPageContext
SEVERE: Exception initializing page context
java.lang.IllegalStateException: Cannot create a session after the response has been committed <------------Which session is my code creating? where? Why is this sporadic?
at org.apache.catalina.connector.Request.doGetSession(Request.java:2377)
at org.apache.catalina.connector.Request.getSession(Request.java:2097)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:833)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:547)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:547)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:547)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:493)
at org.apache.jasper.runtime.PageContextImpl._initialize(PageContextImpl.java:146)
at org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:124)
at org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:107)
at org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:63)
at org.apache.jsp.WEB_002dINF.pages.utility.Error_jsp._jspService(Error_jsp.java:49) <-------------- This is the JSP page where all my JSP pages redirect when something goes wrong
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:377)
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.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.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:621)
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:820)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
at org.apache.jsp.WEB_002dINF.pages.ProductsForModel_jsp._jspService(ProductsForModel_jsp.java:1137) <------- In this case, the problem happened in my page "ProductsForModel.jsp"
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:377)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1056)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:388)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:231)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.zlatkovic.HttpRedirectFilter.doFilter(HttpRedirectFilter.java:229)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
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.valves.AccessLogValve.invoke(AccessLogValve.java:555)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:276)
at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:81)
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(Thread.java:619)
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Brian session is enabled on JSP pages unless you disable them. Have you written session="false" in the page directive of ProductsForModel.jsp and then in the code you are using the session??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Brian Braun Mitman
Greenhorn

Joined: May 26, 2010
Posts: 14
Hi Ankit,

I haven't used that directive in any of the JSP pages, not at all, because I certainly use the session in all my code.
Any other ideas?

Thanks a lot for your response!
Brian Braun Mitman
Greenhorn

Joined: May 26, 2010
Posts: 14
In the message "java.lang.IllegalStateException: Cannot create a session after the response has been committed ", what does "response has been commited" mean?
What exactly is to "commit a response"? To start creating the response even before it has been delivered to the client's browser? When is a response considered "commited" exactly?
Brian Braun Mitman
Greenhorn

Joined: May 26, 2010
Posts: 14

Hi again,

Regarding my initial question that started this topic, I have some questions/doubts/ideas/etc:


1 - Question: What does "response has been commited" mean? I have a theory: Maybe it means that in the java code that the container generated for my JSP, at least one "out.write()" method has already been used, which means that "It already has started to send html code to the browser". Am I right?

2- I have found that request.getSession() is the same as request.getSession(true), which means that it will create a session if it doesn't exist. I thought that no argument meant "false".

3- In the java code that the container generated for my JSP, I see that the container generated this line among the first lines in the "_jspService()" method:

session = pageContext.getSession();

That made me remind that, in fact, a variable called "session" is always available for my script in a JSP page, and I deduct that in that line is were this variable is being assigned. I had forgot that!
Well, in my JSP pages I have some script at the end, that performs several "request.getSession()"s. Maybe I should not do that, maybe I should use the "session" variable that has been assigned in the beginning automatically. And MAYBE that is the problem. MAYBE in my script one of this "request.getSession()"s is executed when the session doesn't exist anymore, so then a new session is created, and maybe that should not happen after at least one "out.write()" has been executed. Do you agree?

4- If that is true, if the session sometimes has been destroyed for some reason, and these "request.getSession()"s tells the container "Hey, create a new session if I don't have one": What could be the reason for a session to not exist anymore when the ".getSession()" is executed? My sessions should last at least one hour, that is the expiration time. And I don't have any ".invalidate()" method in my code. I don't understand why is the session inexistent sometimes. The JSP creates a session if it doesnt exist at the beginning of the java code that the container creates for my JSP, so it should last while the "_jspService()" method performs. And that method performs in just a few seconds, no more than 10 or 20 in the worst case, if ever that happens.

Thanks in advance for your responses!
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
response has been commited:
request,response objects are thread safe and synchronous in nature(till Apache tomcat6.0).
Be careful with respect to the use of response.sendRedirect() and forward()-->A case where illegalstateException might have raised.
> or buffer might be flushed explicitly.
Brian Braun Mitman
Greenhorn

Joined: May 26, 2010
Posts: 14
Hi Dileep,

It seems that it has been solved :-)

In a few more days I will be able to be sure, but it seems that the buffer was getting full. I raised it from 8K (default) to 64K, and now it seems that the "....response has been commited" exception is gone.
It seems that after that happened, the error page (to which all pages redirect) had a problem creating a session. I modified the error page so it wont try to create a session.

As a summary: My error is is not trying to create a session anymore (which seemed to happen after the "response had been commited"), and my buffer seems to be enough now (which seemed to get full, and trigger a coomit of the response for some reason).
pham thanh tung
Greenhorn

Joined: Aug 15, 2011
Posts: 7
But current i use JSF, so how add PAGE directive?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56180
    
  13

pham thanh tung, this forum is for questions on Struts. Please post any JSF questions in the JSF forum.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: java.lang.IllegalStateException: Cannot create a session after the response has been committed
 
Similar Threads
jsf in a jsp:include doesn't work?
java.lang.IllegalStateException: Cannot create a session after the response has been committed
javax.servlet.jsp.JspException in Application
java.lang.IllegalStateException: Cannot create a session after the response has been committed
java.lang.IllegalStateException: Cannot create a session after the response has been committed