This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling close() on ServletOutputStream

 
tom walters
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a ServletOutputStream in my doPost method obtained via response.getOutputStream(). Should we call close() on it at the end of the doPost/doGet method? I remember reading somewhere that we should not call close() on a ServletOutputStream and instead only call flush().
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18020
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My experience is that sometimes the container can behave in strange ways, and I have, in deperation, explicitly closed or flushed the output stream, but technically, you shouldn't close it, and I get the impression that flush() may be being overused as well.
However, calling close() on an already closed stream is supposed to have no effect, so the fact that BOTH you and the container close the stream is - in theory - OK.
In practice, you are living dangerously, since the container might conceivably find a reason to put something in the stream AFTER you exit the servlet method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic