This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick fix for Response already committed exception  RSS feed

 
alrem mashayekhi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you getting the response as follows?
What are you doing with it?
Are you writing to the response prior to redirecting?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you can't write to the OutputStream piped to the client browser anymore once you have called:


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:


sendRedirect

void sendRedirect(java.lang.String location)
throws java.io.IOException

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.

Parameters:
location - the redirect location URL
Throws:
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


 
Simon Roberts
Author
Ranch Hand
Posts: 176
9
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!