Use of Java Web Listener (1) Listening for context and session information, Java Web Listener
1. Listener context class
1 package com. examp. ch9; 2 3 import java. io. fileOutputStream; 4 import java. io. printWriter; 5 import java. util. date; 6 7 import javax. servlet. servletContext; 8 import javax. servlet. servletContextAttributeEvent; 9 import javax. servlet. servletContextAttributeListener; 10 import javax. servlet. servletContextEvent; 11 import javax. servlet. servletContextListener; 12 13 public final class implements ServletContextListener, listener {16 private ServletContext context = null; 17 18 public void contextDestroyed (ServletContextEvent sce) 19 {20 logout ("Call contextDestroyed () method --> ServletContext is destroyed "); 21 this. context = null; 22} 23 24 public void contextInitialized (ServletContextEvent sce) 25 {26 this. context = sce. getServletContext (); 27 logout ("Call the contextInitialized () method --> ServletContext initialization"); 28} 29 30 public void attributeAdded (ServletContextAttributeEvent scae) 31 {32 logout ("Call attributeAdded ('" + scae. getName () + "','" + scae. getValue () + 33 "') Method --> added an attribute"); 34} 35 36 public void attributeRemoved (ServletContextAttributeEvent scae) 37 {38 logout ("attributeRemoved ('" + scae. getName () + "','" + scae. getValue () + 39 "') Method --> Delete this attribute"); 40} 41 42 public void attributeReplaced (ServletContextAttributeEvent scae) 43 {44 logout ("attributeReplaced ('" + scae. getName () + "','" + scae. getValue () + 45 "') Method --> changed this attribute"); 46} 47 48 private void logout (String message) 49 {50 PrintWriter out = null; 51 try52 {53 out = new PrintWriter (new FileOutputStream ("E :\\ contextLog.txt", true); 54 out. println (new Date (). toLocaleString () + message); 55 out. close (); 56} 57 catch (Exception e) 58 {59 out. close (); 60 e. printStackTrace (); 61} 62} 63}
2. Classes for listening to Http sessions
1 package com. examp. ch9; 2 3 import java. io. fileOutputStream; 4 import java. io. printWriter; 5 import java. util. date; 6 7 import javax. servlet. servletContext; 8 import javax. servlet. servletContextEvent; 9 import javax. servlet. http. httpSessionAttributeListener; 10 import javax. servlet. http. httpSessionBindingEvent; 11 import javax. servlet. http. httpSessionEvent; 12 import javax. servlet. http. httpSessionListener; 13/* 14 * HttpSessionListener Http session creation and destruction 15 * Http sessionattributelistener listens for changes in session attributes 16 */17 public final class MySessionListener implements listener, HttpSessionListener18 {19 ServletContext context; // create a context object 20 int users = 1; // The number of initial users is 121/* 22 * this operation is triggered when an object is added to the session. In general, this operation is triggered by calling the setAttribute Method 23*24 */25 public void. attributeAdded (HttpSessionBindingEvent event) 26 {27 logout ("attributeAdded ('" + event. getSession (). getId () + "','" + 28 event. getName () + "','" + event. getValue () + "')"); 29} 30/* 31 * this operation is triggered when you modify or delete an object added to a session. In general, the 32*33 */34 public void attributeRemoved (httpSessionBindingEvent event) 35 {36 logout ("attributeRemoved ('" + event. getSession (). getId () + "','" + 37 event. getName () + "','" + event. getValue () + "')"); 38} 39/* 40 * when the Session attribute is reset 41*42 */43 public void attributeReplaced (HttpSessionBindingEvent se) 44 {45 logout ("attributeReplaced ('" + se. getSession (). getId () + ", '" + se. getName () + "','" + se. getValue () + "')"); 46} 47 48 49 50/* 51 * triggering when creating a session is also the first time that the client triggers 52 */53 public void sessionCreated (HttpSessionEvent event) when interacting with the server) 54 {55 System. out. println (users); 56 this. users + = 1; // get the ID and number of users 57 logout ("sessionCreated ('" + event. getSession (). getId () + "'), currently" + this. users + "users"); 58 this. context. setAttribute ("users", new Integer (this. users )); // saves the number of users to context59} 60/* 61 * to destroy the session. Generally, only one button is triggered to destroy the session, or 62*63 */64 public void sessionDestroyed (HttpSessionEvent event) is scheduled to be destroyed) 65 {66 this. users --; 67 logout ("sessionDestroyed ('" + event. getSession (). getId () + "'), currently" + this. users + "users"); // obtain the ID and number of users 68 this. context. setAttribute ("users", new Integer (this. users); // saves the number of users to context69} 70 71 public void contextDestroyed (ServletContextEvent sce) 72 {73 logout ("contextDestroyed () --> ServletContext is destroyed "); 74 this. context = null; 75} 76 77 public void contextInitialized (ServletContextEvent sce) 78 {79 this. context = sce. getServletContext (); 80 logout ("contextInitialized () --> ServletContext initialized"); 81} 82 83 private void logout (String message) 84 {85 PrintWriter out = null; 86 try87 {// create input stream Write File 88 out = new PrintWriter (new FileOutputStream ("E: \ sessionLog.txt", true); 89 out. println (new Date (). toLocaleString () + "-->" + message); 90 out. close (); 91} 92 catch (Exception e) 93 {94 out. close (); 95 e. printStackTrace (); 96} 97} 98}
View Code
3. Front-end JSP file
Context_test.jsp
1 <%@ page contentType="text/html;charset=UTF-8"%>2 <%3 out.println("add attribute");4 getServletContext().setAttribute("userName","Smith");5 out.println("replace attribute");6 getServletContext().setAttribute("userName","Kate");7 out.println("remove attribute");8 getServletContext().removeAttribute("userName");9 %>
Session_test.jsp
1 <% @ page contentType = "text/html; charset = UTF-8" %> 2: 3 session. setAttribute ("userName", "Smith") <br> 4 <% session. setAttribute ("userName", "Smith"); %> <! -- Add Property --> 5 session. setAttribute ("userName", "Kate") <br> 6 <% session. setAttribute ("userName", "Kate"); %> <! -- Change attributes --> 7 session. removeAttribute ("userName", "Kate") <br> 8 <% session. removeAttribute ("userName"); %> <! -- Delete attribute --> 9 currently has <% = getServletContext (). getAttribute ("users") %> users. <Br> 10 after session. invalidate () <br> 11 <% session. invalidate (); %> <! -- Destroy this session --> 12 currently has <% = getServletContext (). getAttribute ("users") %> users.