File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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 =
and I get the error

java.lang.IllegalStateException: Cannot forward after response has been committed
what am I doing wrong
Thank you for your time
Dominik Ratajski

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
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
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.
It is sorta covered in the JavaRanch Style Guide.
subject: Cannot forward after response has been committed error
It's not a secret anymore!