Listener接口和Event类: Listener接口 Event类 ServletContextListener ServletContextEvent ServletContextAttributeListener ServletContextAttributeEvent HttpSessionListener HttpSessionEvent HttpSessionActivationListener HttpSessionAttributeListener HttpSessionBingdingEvent HttpSessionBindingListener ServletRequestLitener ServletRequestEvent ServletRequestAttributeListener ServletRequestAttributeEvent 监听Servlet上下文: 1、 ServletContextListener接口主要监听ServletContext的创建和删除。它提供了两个方法,也称“Web应用程序的生命周期方法“。 contextInitialized(ServletContextEvent event)方法:通知正在收听的对象应用程序已经被加载及初始化。 contextDestroyed(ServletContextEvent event)方法:通知正在收听的对象应用程序已经被注销。 2、 ServletContextAttributeListener接口主要监听ServletContext属性的增加、删除及修改。它提供了三个方法: attributeAdded(ServletContextAttributeEvent event)方法:若有对象加入到application的范围,通知正在收听的对象。 attributeRepleaced(ServletContextAttributeEvent event)方法:若在application范围内一个对象取代另一个对象,通知正在收听的对象。 attributeRemoved(ServletContextAttributeEvent event)方法:若有对象从application范围移除,通知正在收听的对象。 监听Http会话: 1、 HttpSessionListener接口监听Http会话的创建及销毁。提供了2个方法: sessionCreated(HttpSessionEvent event)方法:通知正在收听的对象,session已经被加载及初始化。 sessionDestroyed(HttpSessionEvent event)方法:通知正在收听的对象,session已经被注销。 2、 HttpSessionActivationListener接口监听Http会话active和passivate情况,提供3个方法: attributeAdded(HttpSessionBindingEvent even)方法:若有对象加入session的范围,通知正在收听的对象。 attributeReplaced(HttpSessionBindingEvent even)方法:如有对象在Session范围内被替换,通知正在收听的对象。 attributeRemoved(HttpSessionBindingEvent even)方法:若有对象在Session范围内被删除,通知正在收听的对象。 3、 HttpSessionBindingListener接口监听Http会话中对象的绑定信息,它是唯一不需要在web.xml中设置Listener的。它提供了2个方法: valueBound(HttpSessionBindingEvent event)方法:当有对象加入Session范围是,会被自动调用。 valueUnBound(HttpSessionBindingEvent event)方法:当有对象从Session范围内删除时,会被自动调用。 4、 HttpSessionAttributeListener接口监听Http会话中属性的设置请求,它提供2个方法: sessionDidActivate(HttpSessionEvent event)方法:通知正在收听的对象,其Session已经变为有效状态。 sessionWillPassivate(HttpSessionEvent event)方法:通知正在收听的对象,其Session已经变为无效状态。 监听Servlet请求: 1、 ServletRequestListener接口监听客户的请求信息。提供2个方法: requestInitalized(ServletRequestEvent event)方法:通知正在收听的对象,ServletRequest已经被加载及初始化。 requestDestroyed(ServletRequestEvent event)方法:通知正在收听的对象,ServletRequest已经被删除。 2、 ServletRequestAttributeListener接口监听客户请求的对象信息:提供3个方法: attributeAdded(ServletRequestAttributeEvent event)方法:若有对象加入到Request范围,通知正在收听的对象。 attributeReplaced(ServletRequestAttributeEvent event)方法:若有对象在Request范围被替换,通知正在收听的对象。 attributeRemoved(ServletRequestAttributeEvent event)方法:如有对象在Request范围被删除,通知正在收听的对象。 监听用户的实例: Student类: package com.wgh; public class Student { private String name; private int age; private String address; //无参构造函数 public void Student(){} //有参构造函数 public void Student(String name,int age,String address){ this.name=name; this.age=age; this.address=address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } 监听Student用户代码: package com.wgh; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; /** * Application Lifecycle Listener implementation class StudentTrace * */ @WebListener public class StudentTrace implements HttpSessionBindingListener { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } /** * Default constructor. */ public StudentTrace() { // TODO Auto-generated constructor stub name=""; } /** * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent) */ public void valueBound(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("上线"+this.name); } /** * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent) */ public void valueUnbound(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("下线"+this.name); } } Jsp页面代码: <%@ page import="java.util.*" %> <%@ page import="com.wgh.*" %> ……省略一部分代码 <% //获得用户名 Student stu=new Student(); StudentTrace st=new StudentTrace(); request.setCharacterEncoding("UTF-8"); String name=request.getParameter("username"); st.setName(name); session.setAttribute("stu", st); session.setMaxInactiveInterval(10); %>