• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Should you close response.getWriter() ?

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic