• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

sendRedirect method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic