A method of using the Session Servlet

The Servlet doGet method:

. 1  protected  void the doGet (the javax.servlet.http.HttpServletRequest Request, Response javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException, IOException {
 2          Request.setCharacterEncoding ( "UTF-. 8" );
 . 3          the response.setContentType ( " text / HTML; charset = UTF-. 8 " );
 . 4          // invoke public methods HttpServletRequest getSession () to get HttpSession object, if not, create one 
. 5          HttpSession session = Request.getSession ();
 . 6          // returns the session object with the specified name of the bound object, or null if there is no (remember to convert the return type of the original Object type object) 
7          Integer accessedCount = (Integer) session.getAttribute ( "accessedCount");
 8         if (accessedCount == null) {
 9             accessedCount = new Integer(0);
10             session.setAttribute("accessedCount", accessedCount);
11         }
12 
13         PrintWriter out = response.getWriter();
14         out.print("创建session成功");
15     }

Servlet request object using the session object and outputs the acquired properties:

. 1 the HttpSession the session = Request.getSession ();
 2          Integer accessedCount = (Integer) session.getAttribute ( "accessedCount" );
 . 3          IF (accessedCount == null ) {
 . 4              accessedCount = new new Integer (0 );
 . 5          } the else {
 . 6              accessedCount = new new Integer (accessedCount.intValue () +. 1 );
 . 7          }
 . 8          // values are updated each time an object need to set up the session attributes 
. 9          session.setAttribute ( "accessedCount" , accessedCount);
 10 
11         PrintWriter out = response.getWriter();
12         out.print("sessionId: " + session.getId() + "<br />");
13         out.print("sessionCreationTime: " + new Date(session.getCreationTime()) + "<br />");
14         out.print("sessionLastAccessedTime: " + new Date(session.getLastAccessedTime()) + "<br />");
15         out.print("被访问的次数: " + session.getAttribute("accessedCount") + "<br />");

 Doubts encountered (resolved):

1, Integer object and can not directly change the value can only be assigned a new object.

Guess you like

Origin www.cnblogs.com/GjqDream/p/11537301.html