aspose file tools*
The moose likes JSP and the fly likes java.lang.illegalstateexception cannot forward. response already committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "java.lang.illegalstateexception cannot forward. response already committed" Watch "java.lang.illegalstateexception cannot forward. response already committed" New topic
Author

java.lang.illegalstateexception cannot forward. response already committed

suchit pandya
Ranch Hand

Joined: Oct 27, 2009
Posts: 43


This is all my JSP is having and still it throws an error "java.lang.illegalstateexception cannot forward. response already committed". Can any one please help me with this?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
That error implies that this included file:


-OR-
Any front-end servlet that had access to the request and response before your JSP started

Has already sent the response headers. Thats the only way you would get that exception.

Bill
suchit pandya
Ranch Hand

Joined: Oct 27, 2009
Posts: 43
Hi Bill
THanks for responding.
Actually it works like this current jsp is just an intermediate page and request is coming from client server. I think client server is redirecting the request.I knew why that error is coming but what could be the solution for it?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Try making this JSP into a Servlet.
That way you will know for certain it isn't writing/committing anything to the response.
Its all java code anyway. No reason for it to be in a JSP.

Responses get committed once you start writing the response. That means you can't forward/redirect once you have started returning an answer.
By default JSP pages have a buffer that gives you a bit of leeway. You can start output, and then forward/redirect - it will just clear the buffer and start from "scratch"

Cases where you get this error
- the buffer fills up and is flushed (default Buffer is 8k)
- the buffer explicitly gets flushed

In terms of fixing the problem
1 - rewrite as a servlet to eliminate possible HTML being output from this JSP page..
2 - move the forward/redirect logic further up so that it runs before the buffer is executed.
3 - another thing to watch out for is if you forward/redirect to another resource, you should not do any other output in a JSP


suchit pandya
Ranch Hand

Joined: Oct 27, 2009
Posts: 43
Thanks every one for you response. The actual problem was there is one more security server on top of our web server which writes to response and then redirects to our server. That's why it was throwing error. And for solution I just set a new page action refereshed the page and it worked. Thanks again for all your comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.illegalstateexception cannot forward. response already committed