aspose file tools*
The moose likes Servlets and the fly likes IllegalStateException in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException in servlet" Watch "IllegalStateException in servlet" New topic
Author

IllegalStateException in servlet

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! I'm receiving this error occasionally(1 out of 4 tries) especially when my servlet is passing quite an amount of data(request.setAttribute) to the JSP. But I this error is reported in my log and seem to happen in the servlet and not the JSP.


Jan 22, 2007 1:45:46 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ViewTimeServ threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:313)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at ph.wp.http.controllers.ViewTimeController.doPost(ViewTimeController.java:112)
at ph.wp.http.controllers.ViewTimeController.doGet(ViewTimeController.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)




here is my code...




I tried searching the forums and google but nothing seems to be solve to my problem. The JSP also outputs a blank(white) screen. Thanks!
[ January 22, 2007: Message edited by: Timothy Sam ]

SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

The error usually means you have sent data to the user as if you were going to send them a page, but then redirected them afterward. I can't really say for sure which line is causing this, but I suspect it might have to do with the session variables being set. Can anyone else spot where he might be producing output to the user before a redirect?


My Blog: Down Home Country Coding with Scott Selikoff
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Thanks Scott! Please help people! I can't seem to figure this on my own...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Which is line 112, 126?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
126 is in bold.



Rahul Bhattacharjee
LinkedIn - Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It appears to be the first forward, but I can't see anything else writing to the output, unless the Servlets are chained? I'd try changing the forward to an include to see what happens.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! Changing from

request.getRequestDispatcher(FORWARD_VIEW_TIME).forward(request, response);

to

request.getRequestDispatcher(FORWARD_VIEW_TIME).include(request, response);

doesn't eliminate the problem...
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You still get this:

java.lang.IllegalStateException: Cannot forward after response has been committed
??
Check you code compiled properly. It should be impossible with the code change. Unless I'm insane. It's possible, you know.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Yes sir, it still does... I tried creating and deploying a new war file now and I still get the error. Would it be helpful if I posted my JSP? Here it is...



[Edit by Dave - broke up some crazy long lines]
[ January 23, 2007: Message edited by: David O'Meara ]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys, I seem to have a slightly different Exception with the above suggestion. It says that it cannot create a session...


Jan 23, 2007 8:07:58 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ViewTimeServ threw exception
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2214)
at org.apache.catalina.connector.Request.getSession(Request.java:2024)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:831)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:842)
at ph.wp.http.controllers.ViewTimeController.doPost(ViewTimeController.java:45)
at ph.wp.http.controllers.ViewTimeController.doGet(ViewTimeController.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Not that it solves the problem, but from the API for getSession(boolean)

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.


Something is definitely committing the response. It could be a filter, chained servlet or just an annoying exception, but it is happening. In your logs, is there ever any other exceptions displayed? Do you have any filters? Does this servlet get called internally (ie rather than directly from the client)?
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Something is definitely committing the response. It could be a filter,


nope... I don't have any filters...

[quote\chained servlet
I think I have one which is called atleast once. But it doesn't display any error on that servlet(the one being called before this servlet that produces errors, the error also only appear after several calls on the very first servlet I mentioned in this thread).

or just an annoying exception, but it is happening.

No other exception appear...


Does this servlet get called internally (ie rather than directly from the client)?


As I sad from my statement above. I do have one which sets something in the session. Here it is:



componentUrl's value would be a servlet url like:

t=/viewtime.do
[ January 23, 2007: Message edited by: Timothy Sam ]
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
Simply Delete all statement.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Hi all. I am getting the following error:-

Servlet failed with Exception
java.lang.IllegalStateException: Cannot forward a response that is already committed
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:125)


I am calling the servlet from directly using url. The jsp after processing (using ejb) sets the session attribute and using RequestDispatcher forwards the response to a JSP page. While doing so, everything works fine but I get this exception on Portal Server. It doesn't hinders with any processing or anything else but may be it will create issue during integration. How to rectify this??
[ January 29, 2007: Message edited by: Palak Mathur ]

Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IllegalStateException in servlet