This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Cannot forward after response has been committed error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cannot forward after response has been committed error" Watch "Cannot forward after response has been committed error" New topic
Author

Cannot forward after response has been committed error

john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
I have the following servlet
public void doPost(HttpServletRequest ...
{
String[] id = request.getParameterValues("id");
String[] itemType = request.getParameterValues("itemType");

int size = id.length;
for(int i = 0; i < size; i++)
{
System.out.println("ID IS " + id[i]);
System.out.println("ITEMTYPE IS " + itemType[i]);
}
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/updateItem.jsp");
rd.forward(request,response);
}
and I get the error

java.lang.IllegalStateException: Cannot forward after response has been committed
ch12.SaveServlet.doPost(SaveServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
what am I doing wrong
Thank you for your time
Dominik Ratajski
Greenhorn

Joined: Feb 09, 2004
Posts: 14
the error relates to you already sending some kind of response (eg response.getWriter().println(...)) back to the browser and then asking it to redirect/forward.
check your code to see whether you're sending any response information prior to the forward. you can only do either, ie one forward or (multiple)write but not combinations of the two or multiples of forwards/redirects
DSR
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Actually the key point is "response has been committed" - this occurs when the output buffer is flushed and some data has actually been sent to the response. So - you can write to the response, but it can't be enough bytes to force flushing the output buffer. In that case, the buffer is cleared before the forward is done. Sometimes people make the response buffer larger in order to prevent this -
See the JavaDocs for the RequestDispatcher.forward() method and the ServleResponse.setBufferSize() for discussion.
Bill
 
wood burning stoves
 
subject: Cannot forward after response has been committed error
 
Similar Threads
jsp error
Jasper Report Export PDF Problem.
response already committed exception when calls to one of my servlet
Dispaching a request from Servlet to JSP ERROR (its urgent please help me)
PDF save in Browser?