java.lang.IllegalStateException: Cannot forward after response has been committed

I encountered this error while working on the final module of my project earlier. The error was obvious to me though that it occured in one of my included JSP's.

<center>
<jsp:include page="/WEB-INF/templates/usermenutemplate.jsp"/>
</center>
<br/>
<br/>
<jsp:include page="/WEB-INF/templates/addpositiontemplate.jsp"/>
<hr/>
<c:import url="/actions/viewpositions.do"/>
<br/>
<br/>



normally, I wouldn't have any problems doing this. But it looks like there was quite a huge amount of processing going on with those included JSP's and even the Servlet from which this set of JSP tags page was forwarded from.

//Sample servlet code that demonstrates how much processing is being done on the servlet.

DBHelper dao = new DBHelper();
    
request.setAttribute("positionTypesList", dao.selectPositionTypes() );
    
return mapping.findForward(FORWARD_managepositions); 

There was no time think of optimizing the code. I had to resolve to a simple hack and maybe it was really the last card that I have.

Problem was actually solved with this single line of code

<%@ page buffer="16kb" %>

I think the default buffer size for JSP is 8kb. When you get too much output to generate more than that it results to a IllegalStateException: Cannot forward after response has been committed. Why? Because I think the buffer is being flushed right away even before the response is completed so we have to set the default buffer size explicitly. Fore more info on how to solve this problem, check out these links.

http://forum.java.sun.com/thread.jspa?threadID=683891&messageID=3983241
http://www.jguru.com/faq/view.jsp?EID=480147
http://www.velocityreviews.com/forums/t151515-servlet-file-download-javalangillegalstateexception-cannot-forward-after-response-has-been-committed.html
http://forum.java.sun.com/thread.jspa?forumID=45&threadID=204859
http://forum.java.sun.com/thread.jspa?threadID=604166
Published 09-05-2006 2:25 AM by lamia
Filed under: , , ,

Comments

Tuesday, September 05, 2006 7:10 AM by cruizer

# re: java.lang.IllegalStateException: Cannot forward after response has been committed

the same kind of problem occurs on other platforms (e.g. PHP), unless you turn on output response buffering. this makes sure that the output is sent to the browser only after all the headers have been sent

Monday, June 18, 2007 1:04 AM by vincent

# re: java.lang.IllegalStateException: Cannot forward after response has been committed

i want solutions for this error

Thursday, January 10, 2008 9:21 PM by Prabhakar

# re: java.lang.IllegalStateException: Cannot forward after response has been committed

I am getting same error while i am forwarding cotrol to next jsp page using RequestDispatcher .even if i m setting <%@ page buffer="256kb" %>.The proble is not yet solved.

so anybody has any solution other than buffer size setup

Thanx in advance

Cheers,

Prabhakar