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 or setcontentlengthlong method of response has been greater 0 , has been written response.
  • the senderror method called.
  • the sendredirect method called.
  • the complete method on asynccontext 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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -