Do I need to flush the servlet outputstream?
Solution 1
You don't need to. The servletcontainer will flush and close it for you. The close by the way already implicitly calls flush.
See also chapter 5.6 of Servlet 3.1 specification:
5.6 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
orsetContentLengthLong
method of the response has been greater than zero and has been written to the response.- The
sendError
method is called.- The
sendRedirect
method is called.- The
complete
method onAsyncContext
is called.
Calling flush while still running the servlet's service is usually only beneficial when you have multiple writers on the same stream and you want to switch of the writer (e.g. file with mixed binary/character data), or when you want to keep the stream pointer open for an uncertain time (e.g. a logfile).
Solution 2
Guess that the same answer you got in your other question applies here: if it is your stream, flush and close it. Otherwise the stream creator should be doing it, unless otherwise stated.
Solution 3
To point out an insidious exception to the rule “no need to flush”: Working with IBM WebSphere Application Server and using the response Writer (rather than the OutputStream) I found that I had to flush it; otherwise a final portion of my response data was lost. I suppose that IBM's HttpServletResponse
class does indeed flush the OutputStream but uses a separate buffer for the Writer and does not flush it. Other application servers seem to do this.
So if you send your response data to the Writer it is safer to flush it. But there is no need to flush the OutputStream into the bargain.
(I would have posted this as a comment but lack the reputation to do it.)
Carlos Aguayo
Updated on October 02, 2020Comments
-
Carlos Aguayo over 3 years
Do I need to "flush" the OutputStream from the HttpServletResponse?
I already saw from to Should I close the servlet outputstream? that I don't need to close it, but it's not clear if I need to flush it. Should I expect it from the container as well?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] response = getResponse(); String responseType = getResponseType(); response.setContentLength(response.length); response.setContentType(responseType); response.getOutputStream().write(response); response.getOutputStream().flush(); // yes/no/why? }
-
KANJICODER almost 6 yearsSomeone mentioned above, if it is your stream, flush it, if not, don't. If I understand correctly, would that poster's rule be applicable here?
-
Renardo almost 6 years@JMADISON: The rule seems to apply to the stream but not to the writer based on that stream. Since flushing does no harm except maybe for throughput / performance I would recommend it for the output writer of a
HttpServletResponse
, regardless of the server type.