• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot forward after response has been committed error

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic