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();
}
}