Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I close the servlet outputstream? [duplicate]

Tags:

java

servlets

Possible Duplicate:
Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()?

Am I responsible for closing the HttpServletResponse.getOutputStream() (or the getWriter() or even the inputstream) or should I leave it to the container ?

protected void doGet(HttpServletRequest request, HttpServletResponse response)     throws ServletException, IOException {     OutputStream o = response.getOutputStream();     ...      o.close(); //yes/no ? } 
like image 748
leeeroy Avatar asked Dec 01 '09 23:12

leeeroy


People also ask

Do you need to close OutputStream?

copy(InputStream, OutputStream) must not close the OutputStream . You can use it for example to concatenate different InputStreams and in this case it would be a bad idea if it would close the provided Input- and/or OutputStream. As a rule of thumb any method should close only those steams it opens.

Should I close HttpServletResponse OutputStream?

The general rule of them is this: if you opened the stream, then you should close it. If you didn't, you shouldn't. Make sure the code is symmetric. In the case of HttpServletResponse , it's a bit less clear cut, since it's not obvious if calling getOutputStream() is an operation that opens the stream.

Is it necessary to close InputStream in Java?

You don't have to close it for all subclasses. For example a java. io. ByteArrayInputStream does not use any file descriptors, and hence closing it is a no-op, see docs.oracle.com/javase/8/docs/api/java/io/…

What is OutputStream in Servlet?

servlet, is an abstract class that provides an output stream to send binary data to the client. ServletOutputStream inherits OutputStream which is the superclass of all classes representing an output stream of bytes. Subclasses of ServletOutputStream class must implement the java. io. OutputStream.


2 Answers

You indeed don't need to do so.

Thumb rule: if you didn't create/open it yourself using new SomeOutputStream(), then you don't need to close it yourself. If it was for example a new FileOutputStream("c:/foo.txt"), then you obviously need to close it yourself.

Reasons that some people still do it are just to ensure that nothing more will be written to the response body. If it would ever happen, then this will cause an IllegalStateException in the appserver logs, but this wouldn't affect the client, so the client still gets the proper response. This is also an easier debug to spot the potential problems in the request-response chain which you wouldn't see at first glance. For example, something else is appending more data to the response body somewhere further down in the chain.

Another reason which you see among starters is that they just wanted to prevent that more data is written to the response body. You see this often when JSP incorrectly plays a role in the response. They just ignore the IllegalStateExceptions in the logs. Needless to say that this particular purpose is bad.

like image 159
BalusC Avatar answered Oct 15 '22 09:10

BalusC


No you don't need to close it. If you do you basically end the response to the client. After closing the stream you cannot send anything else to the client until the next request. You didn't open the stream, so you don't have to close it.

like image 30
Jeremy Raymond Avatar answered Oct 15 '22 08:10

Jeremy Raymond