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