Meaningless Drivel is fun!
The moose likes Servlets and the fly likes HTTPServletResponse sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTPServletResponse sendRedirect" Watch "HTTPServletResponse sendRedirect" New topic

HTTPServletResponse sendRedirect

vivek makode

Joined: Oct 01, 2001
Posts: 27
Hi All,
Why and when does this method throw an IllegalStateException?
The API says this...."If the response has already been committed, this method throws an IllegalStateException" .
What actually is meant by response already committed? how can i solve this problem?
KIndly respond. This exception has been bugging me since long in my current project.
Thanking you in advance.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The response is comitted as soon as you start sending data to the client. This isn't quite straigh-forward, since the first stuff you send to the client is usually buffered by the output stream, so you may be able to write some data before this buffer fills and the response is comitted.
What this means is that as soon as you begin responding with the contents of a page, you can't suddenly change your mind and decide to send something different. It's too late. Fot this reason it's best to have a controller that decides where the output will come from, but doesn't send any data.
Some people think that you can get around this by increasing the size of the buffer and not using a controller, but this is pretty weak code. It's far better to create a stable solution first time around. One important point is that some actions may cause the buffer to flush before it fills, and in this case it doesn't matter how big the buffer is.
I agree. Here's the link:
subject: HTTPServletResponse sendRedirect
It's not a secret anymore!