• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How servlet response is commited?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic