While reading the documentation for sendRedirect method , i came across this -
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response shou
ld be considered to be committed and should not be written to
Can anyone explain me what does exactly committing a response means?
For example writing anything to the servletOuputStream or printWriter will commit the response (there is a slight exception if the output is buffered but ignore that for now). Also if any headers are sent to the client the response should be considered committed.
A call to response.sendRedirect would send headers to the browser telling it that it should make a new request with a different URL. Once this is in motion, it would be pointless to send any output to the client from the current page because the browser is already making a request for a new page.
Some simple ways to avoid this:
Don't call dispatcher.forward or sendRedirect after you've written anything to the outputStream or prrintWriter. Likewise, don't try to write anything to the outputStream or printWriter after you've called one of these methods.
Also, don't call setHeader after you've already started writing to the outputStream or printWriter.
In this case, you've told the container to forward context to another resource and then tried to write to the printWriter.
To prevent this, either branch your code in such a way that one or the other gets called:
Or, add a call to return to halt the execution of this method before any code that tried to write to the printWriter can be called.
Originally posted by D Rog:
Startin writing is not equal committed. You should know that. Actually committed means that response content length is sent out and client will expect to read no longer than that. When you use chunked transfer encoding, a client can accept additional headers after last chunk of content and send redirect headers can be there.
Hi, D Rog.
Would you mind sharing your source for that?
I wasn't aware that you could start writing the body of a response without first setting the headers and response code.
From reading isCommitted in the API
public boolean isCommitted()
Returns a boolean indicating if the response has been committed. A committed response has already had its status code and headers written.
And, from section SRV.5.1 in the servlet spec (2.4):
The isCommitted method returns a boolean value indicating whether any response bytes have been returned to the client.
Is there another document that I'm missing?
Maybe you were thinking about a buffered response which allows you to start writing to the printWriter and then clear it to send a responseRedirect?
I did mention that in my first post but only in passing as not to confuse the issue.
(there is a slight exception if the output is buffered but ignore that for now)
[ March 01, 2008: Message edited by: Ben Souther ]
Originally posted by D Rog:
Actually committed means that response content length is sent out and client will expect to read no longer than that. When you use chunked transfer encoding, a client can accept additional headers after last chunk of content and send redirect headers can be there.
Also, when sending chunked data in HTTP 1.1, you're not supposed to set a content-length header at all.
From SRV 22.214.171.124 in the servlet spec:
When using HTTP 1.1 chunked encoding (which means that the response has
a Transfer-Encoding header), do not set the Content-Length header.