This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring 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

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: 13027
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.
I agree. Here's the link:
subject: Cannot forward after response has been committed error
It's not a secret anymore!