Do I need to flush the servlet outputstream?
Asked Answered
C

4

36

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?
}
Coronal answered 18/2, 2011 at 16:10 Comment(0)
A
53

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 or setContentLengthLong 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 on AsyncContext 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).

Alliance answered 18/2, 2011 at 18:23 Comment(0)
T
5

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.

Turtleback answered 18/2, 2011 at 16:14 Comment(0)
T
4

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

Tragic answered 12/2, 2018 at 14:47 Comment(2)
Someone 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?Slantwise
@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.Tragic
S
-1
java.lang.Object
  extended byjava.io.Writer
      extended byjavax.servlet.jsp.JspWriter


close
public abstract void close()
                    throws IOException
Close the stream, flushing it first. 
This method needs not be invoked explicitly for the initial JspWriter as the code generated by the JSP container will automatically include a call to close(). 

Closing a previously-closed stream, unlike flush(), has no effect. 


Throws: 
IOException - If an I/O error occurs

============================

So, DO NOT close the output stream explicitly.
Snowberry answered 14/10, 2014 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.