On Mon, May 25, 2009 at 2:21 PM, Brian <[email protected]> wrote:
> > It seems about a 50/50 split in hello world examples of HttpServlet > out on the 'internets' as to whether or not to close the > ServletOutputStream or PrintWriter. NetBeans for example will put the > close() call in their new Servlet wizard by default. So does Oracle's > JDeveloper. But other tutorials/examples do not close it. > http://edocs.bea.com/wls/docs70/servlet/progtasks.html > > In my particular case not closing the ServletOutputStream solved an > 'odd behavior' bug in an application when running on HTTPS on a > somewhat 'black box' production environment that was using Oracle Web > Cache and several other pre and post processing technologies on each > request. But I have not been successful at articulating why not > closing the outputstream is the fix or verifying there are no other > consequences as a result of not closing the output stream. 99% of the > time it seems to matter little whether the close is there are not (e.g > every J2EE IDE I have tried), but in this case it did Any thoughts? > > public class HelloWorldServlet extends HttpServlet { > public void service(HttpServletRequest req, > HttpServletResponse res) > throws IOException > { > // Must set the content type first > res.setContentType("text/html"); > // Now obtain a PrintWriter to insert HTML into > PrintWriter out = res.getWriter(); > out.println("<html><head><title>" + > "Hello World!</title></head>"); > out.println("<body><h1>Hello World!</h1></body></html>"); > //QUESTION: close or not close: > //out.close(); > } > } I say: Don't close. The lifecycle of the request is up to the container to manage. (and also, it could potentially damage Filters processing what's produced by the Servlet) Just my 2 cents, > > > > > -- Viktor Klang Rockstar Developer --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---
