Some Text Before .....Some Text After.....
Once the flush method is called, the response is committed and further writes to the response are ignored
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
By “committed”, they mean that the response has been sent. That just means the data has been flushed to the stream
SCJP5.0, SCWCD5.0, OCEJPAD6, OCPJWSD6
After flush() we can write data to the buffer(at the end it will write to the response object)
Calling flush() on the PrintWriter commits the response.
when does the client actually get the response i.e when does the container sends the response to the client?
when the thread leaves the doGet method or the service method of HttpServlet class ?
Calling flush has no effect on sending the response to the client ?
SCJP5.0, SCWCD5.0, OCEJPAD6, OCPJWSD6
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP5.0, SCWCD5.0, OCEJPAD6, OCPJWSD6
I think at the end of thread.
I think at the end of service() method, because it is the last stack frame of the thread.
SCJP5.0, SCWCD5.0, OCEJPAD6, OCPJWSD6
Chinmaya Chowdary wrote:Hi Hasnain.
Have you tested the code given by 'Ankit'? If yes, what output you got ? Can you please provide your container/server, browser details?
SRV.5.5 Closure of Response Object
When a response is closed, the container must immediately flush all remaining
content in the response buffer to the client. The following events indicate that the
servlet has satisfied the request and that the response object is to be closed:
• The termination of the service method of the servlet.
• The amount of content specified in the setContentLength method of the response
has been written to the response.
• The sendError method is called.
• The sendRedirect method is called.
If we don't do a sendError or a redirect, the response is sent to the client at the end of the service method of the servlet, specifically the service method of HttpServlet class (kindly correct me if I'm wrong)
SCJP5.0, SCWCD5.0, OCEJPAD6, OCPJWSD6
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Chinmaya Chowdary wrote:Hi Hasnain. Thankyou.
If we don't do a sendError or a redirect, the response is sent to the client at the end of the service method of the servlet, specifically the service method of HttpServlet class (kindly correct me if I'm wrong)
As far my knowledge concerned, if we do sendError() or sendRedirect() the response is sent to the client at the end of the service method. To test this use after sendRedirect() or sendError(). And also do test with System.out.println() in redirectable servlet or jsp.
In overridden http req/resp service.......Srv1
SERVING GET : Srv1
Leaving doGet().....Srv1
Leaving overridden http req/resp service.......Srv1
Leaving overridden servlet req/res service.......Srv1
In overridden http req/resp service.......Srv2
SERVING GET : Srv2
Leaving doGet()..... Srv2
Leaving overridden http req/resp service.......Srv2
Leaving overridden servlet req/res service.......Srv2
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
Ankit Garg wrote:I used Tomcat 6 and Firefox 3.5 and got 6 Hello first and then 2 hello. I also got 8 hello at once a few times but the ratio was like 50:50. Half of the times I was getting 6 and 2; and half of the times I was getting 8 altogether. I believe that the response is sent when flush is called (the browser might not render it right away). This is why we get an IllegalStateException of we try to use a sendRedirect or sendError etc after flushing the stream...
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |