• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Quick fix for Response already committed exception

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Author
Posts: 310
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic