Forums Register Login

error: response already committed

+Pie Number of slices to send: Send
hi
friends,
I have written code like this in JSP page

// lot of code is there
try{
/**
lot of code is there
*/
response.sendRedirect("_otherPage?name=value");
}
catch(Exception e)
{
System.out.println("error:" + e.getMessage());
}

I am running this code in Web Logic Server
At the point of
response.sendRedirect("_otherPage?name=value");
server giving error like this
error: response already committed
I am facing this problem since two days back. Before that it was working fine.
I restarted server also.

Any body could understand the problem?
Expecting responses from u.
thanks in advance
regds
mohan
+Pie Number of slices to send: Send
you probably have this jsp page included in another jsp page.
in that case, you can't use response.sendRedirect() because the response is already committed hence, the error...
+Pie Number of slices to send: Send
sendRedirect doesn't halt the execution of the service method on it's own.
A good rule of thumb is to always follow sendRedirect (and requestDispatcher.forward) with a return statement unless you have follow up code that you want to run.

+Pie Number of slices to send: Send
 

/**
lot of code is there
*/
response.sendRedirect("_otherPage?name=value");
}



The problem is your "lot of code here".

A redirect depends upon being able to send response headers to the browser (which tell the browser to perform the redirect). Since the headers must be sent prior to any content, once any content at all has been sent, the opportunity for sending headers is gone. Therefore, once any content at all has been flushed, the response is considered "committed" and you can't do anything (including redirects) that involve headers.

You best bet is to make any redirect decisions before doing anything that might cause the response to commit -- or better yet, factor your processing out of the JSP page and into a controller servlet.
[ May 31, 2005: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
You know, if Ben and I keep bumping heads like this, we're going to end up joined at the foreheads!
+Pie Number of slices to send: Send
 

Originally posted by Bear Bibeault:
You know, if Ben and I keep bumping heads like this, we're going to end up joined at the foreheads!



Hehe..

To sum up all the responses....
Your code should:
  • Process the request
  • Determine whether or not to redirect
  • If Yes, redirect and

  • Either return immediately or be careful not to touch the output stream
    Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 2276 times.
    Similar Threads
    java.lang.IllegalStateException: Response already committed
    Redirect page
    Response.sendRedirect()
    Response already committed
    bypassing interceptors
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 04:32:17.