• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confused with flush() behavior

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Could some one kindly explain why the following code does not ignore anything written to the response once the flush method has been called on the PrintWriter Object even when uncommenting



in the code snippet below



Once the flush method is called, the response is committed and further writes to the response are ignored (kindly correct me if I'm wrong). I'm getting


Some Text Before .....Some Text After.....



in the browser.

Thanks in advance.
Kind Regards.
Hasnain.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Once the flush method is called, the response is committed and further writes to the response are ignored



Nope, this is the behavior of close method...
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit.
Thanks for the reply. The below line is from HFSJ Chapter 4 page 137 (Watch It section)


By “committed”, they mean that the response has been sent. That just means the data has been flushed to the stream



Confused .

Kind regards.
Hasnain.
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hasnain.

As far my knowledge concerned, when we call 'flush()' method, whatever data that is present in the buffer(which is typically of size 64kb) will be committed(means adds it to the response object) and if we set any headers or status codes they also be committed. After flush() we can write data to the buffer(at the end it will write to the response object), but we cann't change the headers or status codes.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chinmaya.
Thanks for the reply.


After flush() we can write data to the buffer(at the end it will write to the response object)



I had a look at the API Doc here J2EE 1.4 Api Docs for method getWriter().


Calling flush() on the PrintWriter commits the response.



So now the question comes down to this. 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 ?

Puzzled

kind Regards.
Hasnain.

 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hasnain.

when does the client actually get the response i.e when does the container sends the response to the client?


I think at the end of thread.

when the thread leaves the doGet method or the service method of HttpServlet class ?



I think at the end of service() method, because it is the last stack frame of the thread.

Calling flush has no effect on sending the response to the client ?


When we call flush() the response will not be sent to the client at that moment.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the content of the buffer are sent to the client when flush is called. This is the whole purpose of flush method. It ensures that anything waiting to be written to a stream is sent through the stream. I wrote a test program for this


I got the first 6 Hellos and after 5 seconds the last 2 hello messages in my browser. The behavior is the same for response.flushBuffer() and out.flush()...
 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit.

I tested this in Tomcat5.5.9 container using Firefox3.5 browser. I got '8 Hello' messages at the same time. Later I increased the thread's sleep time period, at this time browser waits for that time period and send '8 Hello' messages at the same time.

I think once the first response(i.e '6 Hello' messages)has sent to the client, I think browser will not wait for the second response because Http is stateless protocol.

Can you please explain, which container/server and browser you are using? Please correct me, if I made mistake.

Thankyou.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chinmaya.

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.



Well according to the code snippet that Ankit gave, It seems that the response is sent before the thread ends or the end of service method .

Kind Regards.
Hasnain.

 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Container/Server: apache-tomcat-6.0.16
Browser: Mozilla Firefox 3.0.14

I ran Ankit's code snippet and got 8 Hello messages at the same time as well (same as what you got). I modified Ankit's code a bit and over rode the service method.

Ankit's Modified code:


Overridden Service Method


I found the following in the Specification.


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)

Kind Regards.
Hasnain.
 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chinmaya.

Thanks for the tip to clear my doubt.

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.



I modified the code a little more and it turns out that you were right. The response is sent to the client at the end of the service method (service(ServletRequest req, ServletResponse res) specifically ). Below are the code snippets.

Redirector Servlet


Redirected Servlet


Following is the output I got on the console


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



I found the following documentation in the J2EE 1.4 API Docs for sendRedirect


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.



The response should be considered to be committed means that Its not guaranteed to be sent until the end of the service method ?

Kind Regards.
Hasnain.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...



Sorry Ankit I did not get your answer .

Kind Regards.
Hasnain.
reply
    Bookmark Topic Watch Topic
  • New Topic