i tested HttpResponse#flushBuffer
and PrintWriter#flush
on Tomcat 7
below, but it seemed that the response rather ignored them than fl
I just had this same issue. To stop browsers waiting till the page finishes loading before it does any rendering you need to start with:
response.setContentType("text/html;charset=UTF-8");
I had that issue, too. And I found, too, that the issue goes away with curl. With some sniffing, it turned out that the culprit is gzip encoding. In order to compress the response, gzip waits until the underlying PrintWriter is closed (that is, until the full response is written) and then produces the compressed output. On the client side, this means that you do not get anything back until the full response is ready. Curl, on the other hand, does not issue an Accept-Encoding: gzip to the server, and that's why the thing works, and you can get the chunked output normally as intended.
The API for flushBuffer() is very precise:
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.
So either Tomcat is not implemented according to the spec (buffers more aggressively and holds flushes if they are too small) or the client (browser) waits for more input before actually rendering it.
Can you try with curl or nc instead?
A yet unmentioned possible cause for this phenomenon is anti-virus software. I did observe the same behavior on my machine: server sending chunked data, curl worked, normal browsers didn't, and also Fiddler confirmed that it wasn't the server who blocked. So after a while I suspected the web protection of Sophos (the anti-virus software that my employer uses) to interfere, and indeed they confirm at https://community.sophos.com/kb/en-us/115656 that they block chunked data until the response is complete, except for certain mime-types that are often used for streaming media data.
As a quick check one can run the HTTP-Push demo at http://www.pushlets.com. If it doesn't work it is quite likely that the client has a general problem with handling chunked data of type text/html properly.