1.监听上下文的类
package com.examp.ch9; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public final class MyServletContextListener implements ServletContextListener, ServletContextAttributeListener { private ServletContext context = null; public void contextDestroyed(ServletContextEvent sce) { logout("调用contextDestroyed()方法-->ServletContext被销毁"); this.context = null; } public void contextInitialized(ServletContextEvent sce) { this.context = sce.getServletContext(); logout("调用contextInitialized()方法-->ServletContext初始化"); } public void attributeAdded(ServletContextAttributeEvent scae) { logout("调用attributeAdded('" + scae.getName() + "', '" + scae.getValue() + "')方法-->增加了一个属性"); } public void attributeRemoved(ServletContextAttributeEvent scae) { logout("调用attributeRemoved('" + scae.getName() + "', '" + scae.getValue() + "')方法-->删除了该属性"); } public void attributeReplaced(ServletContextAttributeEvent scae) { logout("调用attributeReplaced('" + scae.getName() + "', '" + scae.getValue() + "')方法-->更改了该属性"); } private void logout(String message) { PrintWriter out = null; try { out = new PrintWriter(new FileOutputStream("E:\\contextLog.txt", true)); out.println(new Date().toLocaleString() + message); out.close(); } catch (Exception e) { out.close(); e.printStackTrace(); } } }
2.监听Http会话的类
package com.examp.ch9; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /* * HttpSessionListener Http会话的创建、销毁 * HttpSessionAttributeListener 监听会话中属性的改变 */ public final class MySessionListener implements HttpSessionAttributeListener, HttpSessionListener { ServletContext context;//创建一个context对象 int users = 1;//初始化用户数量为1 /* * 在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的 * */ public void attributeAdded(HttpSessionBindingEvent event) { logout("attributeAdded('" + event.getSession().getId() + "', '" + event.getName() + "', '" + event.getValue() + "')"); } /* * 修改、删除session中添加对象时触发此操作 笼统的说就是调用 removeAttribute这个方法时候会触发的 * */ public void attributeRemoved(HttpSessionBindingEvent event) { logout("attributeRemoved('" + event.getSession().getId() + "', '" + event.getName() + "', '" + event.getValue() + "')"); } /* * 在Session属性被重新设置时 * */ public void attributeReplaced(HttpSessionBindingEvent se) { logout("attributeReplaced('" + se.getSession().getId() + ",'" + se.getName() + "','" + se.getValue() + "')"); } /* * 新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发 */ public void sessionCreated(HttpSessionEvent event) { System.out.println(users); this.users += 1;//获取ID 和用户个数 logout("sessionCreated('" + event.getSession().getId() + "'),目前有" + this.users + "个用户"); this.context.setAttribute("users", new Integer(this.users));//将用户数存入context } /* * 销毁会话的时候 一般来说只有某个按钮触发进行销毁 或者配置定时销毁 * */ public void sessionDestroyed(HttpSessionEvent event) { this.users --; logout("sessionDestroyed('" + event.getSession().getId() + "'),目前有" + this.users + "个用户");//获取ID 和用户个数 this.context.setAttribute("users", new Integer(this.users));////将用户数存入context } public void contextDestroyed(ServletContextEvent sce) { logout("contextDestroyed()-->ServletContext被销毁"); this.context = null; } public void contextInitialized(ServletContextEvent sce) { this.context = sce.getServletContext(); logout("contextInitialized()-->ServletContext初始化了"); } private void logout(String message) { PrintWriter out = null; try { //创建输入流 写入文件 out = new PrintWriter(new FileOutputStream("E:\\sessionLog.txt", true)); out.println(new Date().toLocaleString() + "-->" + message); out.close(); } catch (Exception e) { out.close(); e.printStackTrace(); } } }
3.前端JSP文件
context_test.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <% out.println("add attribute"); getServletContext().setAttribute("userName","Smith"); out.println("replace attribute"); getServletContext().setAttribute("userName","Kate"); out.println("remove attribute"); getServletContext().removeAttribute("userName"); %>
session_test.jsp
<%@ page contentType="text/html;charset=UTF-8"%> 执行了以下的操作: session.setAttribute("userName","Smith")<br> <% session.setAttribute("userName","Smith");%><!--添加属性--> session.setAttribute("userName","Kate")<br> <% session.setAttribute("userName","Kate");%><!--更改属性--> session.removeAttribute("userName","Kate")<br> <% session.removeAttribute("userName");%><!--删除属性--> 目前有<%=getServletContext().getAttribute("users")%>个用户。<br> after session.invalidate()<br> <% session.invalidate();%><!--销毁该session--> 目前有<%=getServletContext().getAttribute("users")%>个用户。