1.監聽上下文的類
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 MyServletContextListener 14 implements ServletContextListener, ServletContextAttributeListener 15 { 16 private ServletContext context = null; 17 18 public void contextDestroyed(ServletContextEvent sce) 19 { 20 logout("調用contextDestroyed()方法-->ServletContext被銷毀"); 21 this.context = null; 22 } 23 24 public void contextInitialized(ServletContextEvent sce) 25 { 26 this.context = sce.getServletContext(); 27 logout("調用contextInitialized()方法-->ServletContext初始化"); 28 } 29 30 public void attributeAdded(ServletContextAttributeEvent scae) 31 { 32 logout("調用attributeAdded('" + scae.getName() + "', '" + scae.getValue() + 33 "')方法-->增加了一個屬性"); 34 } 35 36 public void attributeRemoved(ServletContextAttributeEvent scae) 37 { 38 logout("調用attributeRemoved('" + scae.getName() + "', '" + scae.getValue() + 39 "')方法-->刪除了該屬性"); 40 } 41 42 public void attributeReplaced(ServletContextAttributeEvent scae) 43 { 44 logout("調用attributeReplaced('" + scae.getName() + "', '" + scae.getValue() + 45 "')方法-->更改了該屬性"); 46 } 47 48 private void logout(String message) 49 { 50 PrintWriter out = null; 51 try 52 { 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.監聽Http會話的類
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會話的創建、銷毀 15 * HttpSessionAttributeListener 監聽會話中屬性的改變 16 */ 17 public final class MySessionListener implements HttpSessionAttributeListener, HttpSessionListener 18 { 19 ServletContext context;//創建一個context對象 20 int users = 1;//初始化用戶數量為1 21 /* 22 * 在session中添加對象時觸發此操作 籠統的說就是調用setAttribute這個方法時候會觸發的 23 * 24 */ 25 public void attributeAdded(HttpSessionBindingEvent event) 26 { 27 logout("attributeAdded('" + event.getSession().getId() + "', '" + 28 event.getName() + "', '" + event.getValue() + "')"); 29 } 30 /* 31 * 修改、刪除session中添加對象時觸發此操作 籠統的說就是調用 removeAttribute這個方法時候會觸發的 32 * 33 */ 34 public void attributeRemoved(HttpSessionBindingEvent event) 35 { 36 logout("attributeRemoved('" + event.getSession().getId() + "', '" + 37 event.getName() + "', '" + event.getValue() + "')"); 38 } 39 /* 40 * 在Session屬性被重新設置時 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 * 新建一個會話時候觸發也可以說是客戶端第一次和服務器交互時候觸發 52 */ 53 public void sessionCreated(HttpSessionEvent event) 54 { 55 System.out.println(users); 56 this.users += 1;//獲取ID 和用戶個數 57 logout("sessionCreated('" + event.getSession().getId() + "'),目前有" + this.users + "個用戶"); 58 this.context.setAttribute("users", new Integer(this.users));//將用戶數存入context 59 } 60 /* 61 * 銷毀會話的時候 一般來說只有某個按鈕觸發進行銷毀 或者配置定時銷毀 62 * 63 */ 64 public void sessionDestroyed(HttpSessionEvent event) 65 { 66 this.users --; 67 logout("sessionDestroyed('" + event.getSession().getId() + "'),目前有" + this.users + "個用戶");//獲取ID 和用戶個數 68 this.context.setAttribute("users", new Integer(this.users));////將用戶數存入context 69 } 70 71 public void contextDestroyed(ServletContextEvent sce) 72 { 73 logout("contextDestroyed()-->ServletContext被銷毀"); 74 this.context = null; 75 } 76 77 public void contextInitialized(ServletContextEvent sce) 78 { 79 this.context = sce.getServletContext(); 80 logout("contextInitialized()-->ServletContext初始化了"); 81 } 82 83 private void logout(String message) 84 { 85 PrintWriter out = null; 86 try 87 { //創建輸入流 寫入文件 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.前端JSP文件
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");%><!--添加屬性--> 5 session.setAttribute("userName","Kate")<br> 6 <% session.setAttribute("userName","Kate");%><!--更改屬性--> 7 session.removeAttribute("userName","Kate")<br> 8 <% session.removeAttribute("userName");%><!--刪除屬性--> 9 目前有<%=getServletContext().getAttribute("users")%>個用戶。<br> 10 after session.invalidate()<br> 11 <% session.invalidate();%><!--銷毀該session--> 12 目前有<%=getServletContext().getAttribute("users")%>個用戶。