File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect method

 
Priyanshu Bahuguna
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic