This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes Calling close() on ServletOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling close() on ServletOutputStream" Watch "Calling close() on ServletOutputStream" New topic

Calling close() on ServletOutputStream

tom walters
Ranch Hand

Joined: Feb 14, 2002
Posts: 34
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

Joined: Jun 25, 2001
Posts: 17257

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.

An IDE is no substitute for an Intelligent Developer.
wood burning stoves
subject: Calling close() on ServletOutputStream
It's not a secret anymore!