java - Do I need to flush the servlet outputstream? -
do need "flush" outputstream httpservletresponse?
i saw should close servlet outputstream? don't need close it, it's not clear if need flush it. should expect container 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? }
you don't need to. servletcontainer flush , close you. close way implicitly calls flush.
see chapter 5.6 of servlet 3.1 specification:
5.6 closure of response object
when response closed, container must flush remaining content in response buffer client. following events indicate servlet has satisfied request , response object closed:
- the termination of
service
method of servlet.- the amount of content specified in
setcontentlength
orsetcontentlengthlong
method of response has been greater 0 , has been written response.- the
senderror
method called.- the
sendredirect
method called.- the
complete
method onasynccontext
called.
calling flush while still running servlet's service beneficial when have multiple writers on same stream , want switch of writer (e.g. file mixed binary/character data), or when want keep stream pointer open uncertain time (e.g. logfile).
Comments
Post a Comment