Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.illegalstateexception cannot forward. response already committed

 
suchit pandya
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic