aspose file tools*
The moose likes Servlets and the fly likes sendRedirect method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect method" Watch "sendRedirect method" New topic
Author

sendRedirect method

Priyanshu Bahuguna
Greenhorn

Joined: Jun 20, 2006
Posts: 5
Hi,

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?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Once the server starts sending a response to the client (browser), the response should be considered committed.

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.


Example:


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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

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.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

isCommitted

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)


-Ben
[ March 01, 2008: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 15.1.2.2 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect method