wood burning stoves 2.0*
The moose likes Servlets and the fly likes How servlet response is commited? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How servlet response is commited?" Watch "How servlet response is commited?" New topic
Author

How servlet response is commited?

swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
Hi all,
Could anybody tell me,how response is commited in servlet?Also I wan to know how calling sendRedirect() on servletResponse throws illegalStateexception after response is commited?
It will be greate , if you can explain it with a code snippet.Any typing mistakes , please ignore.

Thanks in advance


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
The text of a response - response headers plus whatever has been written by the servlet to an output stream - is held until either:
1. the flush() method of the output stream is called
-or-
2. the output buffer gets filled and flushed

At that point, since the response headers have been sent, it is impossible to modify them so trying to do so causes the exception.

There is no need for a code snippet, this is all covered in the servlet Javadocs an API documentation.

Bill
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
Thanks William.
I have one more doubt.I understood that calling sendRedirect() after committing response causes IllegalStateException.
I tried this example from SCWCD book of Hanumant Deshmukh.But it does not throw any exception.Check it out..

public void doGet(HttpServletRequest req, HttpServletResponse res)
{
PrintWriter pw = res.getWriter();
pw.println("<html><body>Hello World!</body></html>");
pw.flush();
res.sendRedirect("http://www.cnn.com");
}
Stein Vom
Ranch Hand

Joined: Jul 10, 2005
Posts: 78
I would also like to know the answer too.
Even this is not showing any error:

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.println("<html><body>Hello World!</body></html>");
out.flush();
out.println("<html><body>Hello World!</body></html>");
response.sendRedirect("http://www.cnn.com");
out.flush();
[ April 23, 2007: Message edited by: Stein Vom ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Stein Vom:
I would also like to know the answer too.
Even this is not showing any error:

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.println("<html><body>Hello World!</body></html>");
out.flush();
out.println("<html><body>Hello World!</body></html>");
response.sendRedirect("http://www.cnn.com");
out.flush();



What are you getting in the client side.The redirected url or the page containing the flushed text ?


Rahul Bhattacharjee
LinkedIn - Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly: as Rahul says, what do you get on the client side?

If it does not fail, then you have to assume that there are ways in which flush() can be called that does not cause the stream to be committed. The most likely way that I can think of is that flush() is a request to the stream to flush but does not actually cause the data to be sent itself, like calling repaint() in AWT.

If you make 400 repaint() calls, it will not necessarily result in paint() being called 400 times. When Java gets a chance it will call paint(). If 50 repaint() requests are waiting you'll only get one paint().

Back to streams, if the stream is managed in a separate thread then this must certainly be the case, otherwise you run the risk of threads blocking on a failed or delayed write.

I'd try flush() then wait for a second or so after it and see if that changes things.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How servlet response is commited?
 
Similar Threads
Enable/Disable Hyperlink (Multiple Submits)
How servlet response is commited?
submiting the output in same page
RequestDispatcher - Response.isCommitted()
what commits the response ?