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 }
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")%>个用户。