• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should you close response.getWriter() ?

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That's pretty basic, but I'll greatly appreciate it if someone could please clarify once and for all:
If HttpServlet.service() invokes response.getWriter (or response.getOutputStream), does it need to close the writer at the end ?



On the one hand, I'm used to closing resources right after having finished with them. One the other hand, who's to tell the broser did not request "KeepAlive", in which case you shouldn't close the stream ?
Or could it be that the servlet container is smart enough to check for "KeepAlive" and if it's present, provide you a Writer whose "close" method has no effect ?

What does the spec say ?

Thanks very much.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you should close the output stream - it will automatically be flush()ed.

This will not affect any keep-alive HTTP connection. I don't know about present implementations, but in early servlet engines, not closing the stream could result in nothing showing up at the browser.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!