ServletOutputStream or PrintWriter in servlet classes. They are created and managed by the web container, and web components should not interfere with its lifecycle. If you do close them inside your servlet service/doGet/doPost methods, it won't cause trouble for a fault-tolerant container.When NetBeans generates a new servlet, it does close the
PrintWriter in processRequest method:If NetBeans insists on closing
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet MyServlet at "
+ request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
out.close();
}
ServletOutputStream, the safe way is to close it in a finally block.In short, don't close servlet
OutputStream or PrintWriter, just as you won't close System.out.Tags: