I have this exception
[9/8/15 8:26:16:493 SGT] 0000002d SystemErr R java.lang.IllegalStateException: Cannot forward. Response already committed.
[9/8/15 8:26:16:493 SGT] 0000002d SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:1146)
[9/8/15 8:26:16:494 SGT] 0000002d SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:191)
What I have in the codes of the application is a single servlet for all actions. Its forwards each commands to specific service classes for processing.
In that single servlet...there is something like this
Whenever response.sendRedirect is called, the exception shows. The forward works normally, without any exceptions but we keep seeing the exception stack trace on the logs. Is there any quick fix to this? we saw this in production and it seem to be flooding the log files and syserr logs. The boss told me this needs to be fixed asap.
Thanks, any help would be appreciated
Find what tries to writes to the Stream after you have called sendRedirect() by looking at the line numbers in the stack trace and arrange that it doesn't when sendRedirect() has been called. That's the hint I get by looking at:
java.lang.IllegalStateException: Cannot forward. Response already committed.
It looks like something is trying to forward your HttpRequest to something else for more handling after you called sendRedirect(). That's why the application still works fine from the user/browser perspective. The exception would occur after the browser has received the redirect response.
sendRedirect() should be the last thing that writes to the output stream and you can't call it either when the OuputStream is already closed because something else already closed it.
Here is the javadoc:
void sendRedirect(java.lang.String location)
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
location - the redirect location URL
java.io.IOException - If an input or output exception occurs
IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL
A.J. Côté wrote:Basically, you can't write to the OutputStream piped to the client browser anymore once you have called:
That's true, of course, as is your follow up info, but I think this poster's problem is actually the other way round. You can't redirect after you've sent output.
In essence, you must decide if this servlet is actually going to send the response, in which case it cannot redirect, or if it's going to redirect, in which case it can't send output.