Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should the ServletOutputStream be closed?

 
Lance Miller
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a Servlet that is used to download some files from a server. The Servlet's logic determines which file is actually sent back, hence the reason the files are not linked to directly.

The Servlet basically reads the files and streams them as the response. In particular, it is using the Servlet's ServletOutputStream retrieved via response.getOutputStream(). My question is this: Should that OutputStream be closed? (I am flushing it.) Am I correct that a Servlet should not close that OutputStream, because that is the "responsibility" of the Servlet Container? And closing it can cause problems such as if a (Servlet) Filter needs to write to the stream?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, the rule is: if you didn't open it, don't close it (unless the documentation tells you to).
 
Lance Miller
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought. I just wanted to check since I see some examples closing it.

Thanks Bear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic