• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error: response already committed

 
mohan kumar nie
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bug Menot
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/**
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, if Ben and I keep bumping heads like this, we're going to end up joined at the foreheads!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic