Skip to content

Commit 4cedd2c

Browse files
committed
Removing try/resources so that the stream is not closed and error handling is reported correctly
1 parent 17f0cfd commit 4cedd2c

File tree

1 file changed

+33
-37
lines changed
  • servlet/event-listeners/src/main/java/org/javaee7/servlet/event/listeners

1 file changed

+33
-37
lines changed

servlet/event-listeners/src/main/java/org/javaee7/servlet/event/listeners/TestServlet.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
public class TestServlet extends HttpServlet {
5555

5656
/**
57-
* Processes requests for both HTTP
58-
* <code>GET</code> and
59-
* <code>POST</code> methods.
57+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
58+
* methods.
6059
*
6160
* @param request servlet request
6261
* @param response servlet response
@@ -66,42 +65,40 @@ public class TestServlet extends HttpServlet {
6665
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
6766
throws ServletException, IOException {
6867
response.setContentType("text/html;charset=UTF-8");
69-
try (PrintWriter out = response.getWriter()) {
70-
out.println("<!DOCTYPE html>");
71-
out.println("<html>");
72-
out.println("<head>");
73-
out.println("<title>Servlet Event Listeners</title>");
74-
out.println("</head>");
75-
out.println("<body>");
76-
out.println("<h1>Servlet Event Listeners</h1>");
77-
out.println("<h2>Setting, updating, and removing ServletContext Attributes</h2>");
78-
request.getServletContext().setAttribute("attribute1", "attribute-value1");
79-
request.getServletContext().setAttribute("attribute1", "attribute-updated-value1");
80-
request.getServletContext().removeAttribute("attribute1");
81-
out.println("done");
82-
out.println("<h2>Setting, updating, and removing HttpSession Attributes</h2>");
83-
request.getSession(true).setAttribute("attribute1", "attribute-value1");
84-
request.getSession().setAttribute("attribute1", "attribute-updated-value1");
85-
request.getSession().removeAttribute("attribute1");
86-
out.println("done");
87-
out.println("<h2>Setting, updating, and removing ServletRequest Attributes</h2>");
88-
request.setAttribute("attribute1", "attribute-value1");
89-
request.setAttribute("attribute1", "attribute-updated-value1");
90-
request.removeAttribute("attribute1");
91-
out.println("done");
92-
out.println("<h2>Invalidating session</h2>");
93-
request.getSession().invalidate();
94-
out.println("done");
95-
out.println("<br><br>Check output in server log");
96-
out.println("</body>");
97-
out.println("</html>");
98-
}
68+
PrintWriter out = response.getWriter();
69+
out.println("<!DOCTYPE html>");
70+
out.println("<html>");
71+
out.println("<head>");
72+
out.println("<title>Servlet Event Listeners</title>");
73+
out.println("</head>");
74+
out.println("<body>");
75+
out.println("<h1>Servlet Event Listeners</h1>");
76+
out.println("<h2>Setting, updating, and removing ServletContext Attributes</h2>");
77+
request.getServletContext().setAttribute("attribute1", "attribute-value1");
78+
request.getServletContext().setAttribute("attribute1", "attribute-updated-value1");
79+
request.getServletContext().removeAttribute("attribute1");
80+
out.println("done");
81+
out.println("<h2>Setting, updating, and removing HttpSession Attributes</h2>");
82+
request.getSession(true).setAttribute("attribute1", "attribute-value1");
83+
request.getSession().setAttribute("attribute1", "attribute-updated-value1");
84+
request.getSession().removeAttribute("attribute1");
85+
out.println("done");
86+
out.println("<h2>Setting, updating, and removing ServletRequest Attributes</h2>");
87+
request.setAttribute("attribute1", "attribute-value1");
88+
request.setAttribute("attribute1", "attribute-updated-value1");
89+
request.removeAttribute("attribute1");
90+
out.println("done");
91+
out.println("<h2>Invalidating session</h2>");
92+
request.getSession().invalidate();
93+
out.println("done");
94+
out.println("<br><br>Check output in server log");
95+
out.println("</body>");
96+
out.println("</html>");
9997
}
10098

10199
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
102100
/**
103-
* Handles the HTTP
104-
* <code>GET</code> method.
101+
* Handles the HTTP <code>GET</code> method.
105102
*
106103
* @param request servlet request
107104
* @param response servlet response
@@ -115,8 +112,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
115112
}
116113

117114
/**
118-
* Handles the HTTP
119-
* <code>POST</code> method.
115+
* Handles the HTTP <code>POST</code> method.
120116
*
121117
* @param request servlet request
122118
* @param response servlet response

0 commit comments

Comments
 (0)