Servlet监听器是Web应用程序事件模型的一部分,当Web应用中的某些状态发生改变时,Servlet容器就会产生相应的事件,比如创建ServletContext对象时触发ServletContextEvent事件,创建HttpSession对象时触发HttpSessionEvent事件,Servlet监听器可接收这些事件,并可以在事件发生前、发生后可以做一些必要的处理。
根据监听对象的不同,Servlet2.4规范将Servlet监听器划分为以下3种:
(1)ServletContext事件监听器:用于监听应用程序环境对象。
(2) HttpSession事件监听器:用于监听用户会话对象。
(3)ServletRequest事件监听器:用于监听请求消息对象。
1.ServletContext事件监听器
对ServletContext对象进行监听的接口有ServletContextAttributeListener和ServletContextListener,其中ServletContextAttributeListener用于监听ServletContext对象中属性的改变,包括增加属性、删除属性和修改属性。ServletContextListener用于监听ServletContext对象本身的改变,例如ServletContext对象的创建和销毁。
ServletContext事件监听器中的接口和方法如表所示:
接口名称 | 方法名称 | 描述 |
ServletContextAttributeListener | attributeAdded(ServletContextAttributeEvent scae) | 增加属性时激发此方法 |
attributeRemoved(ServletContextAtributeEvent scae) | 删除属性时激发此方法 | |
attributeReplaced(ServletContextAttributeEvent scae) | 修改属性时激发此方法 | |
ServletContextListener | contextDestroyed(ServletContextEvent sce) | 销毁ServletContext时激发此方法 |
contextInitialized(ServletContextEvent sce) | 创建ServletContext时激发此方法 |
2.HttpSession事件监听器
对会话对象进行监听的接口有HttpSessionAttributeListener、HttpSessionListener、HttpSessionActivationListener和HttpSessionBindingListener。
其中HttpSessionAttributeListener用于监听HttpSession对象中属性的改变。
HttpSessionListener用于监听HttpSession对象的改变。
HttpSessionActivationListener用于监听HttpSession对象的状态.。
HttpSessionBindingListener用于监听HttpSession对象的绑定状态。
HttpSession事件监听器中的接口和方法如表所示:
接口名称 | 方法名称 | 描述 |
HttpSessionAttributeListener | attributeAdded(HttpSessionBindingEvent hsbe) | 増加属性时激发此方法 |
attributeRemoved(HttpSessionBindingEvent hsbe) | 刪除属性时激发此方法 | |
attributeReplaced(HttpSessionBindingEvent hsbe) | 修改属性时激发此方法 | |
HttpSessionListener | sessionCreated(HttpSessionEvent hse) | 创建HttpSession时激发此方法 |
sessionDestroyed(HttpSessionEvent hse) | 销毁HttpSession时激发此方法 | |
HttpSessionActivationListener | sessionDidActivate(HttpSessionEvent se) | 激活HttpSession时激发此方法 |
sessionWillPassivate(HttpSessionEvent se) | 钝化HttpSession时激发此方法 | |
HttpSessionBindingListener | valueBound(HttpSessionBindingEvent hsbe) | 调用setAttribute()方法时激发此方法 |
valueUnbound(HttpSessionBindingEvent hsbe) | 调用removeAttribute()方法时激发此方法 |
3.ServletRequest事件监听器
对请求消息对象进行监听的接口有ServletRequestListener和ServletRequestAttributeListener,其中ServletRequestListener用于监听ServletRequest对象的变化。ServletRequestAttributeListener用于监听ServletRequest对象中属性的变化。ServletRequest事件监听器的接口和方法如表所示: ServletContext事件监听器中的接口和方法如表所示:
接口名称 | 方法名称 | 描述 |
ServletRequestAttributeListener | attributeAdded(ServletRequestAttributeEvent srae) | 増加属性时激发此方法 |
attributeRemoved(ServletRequestAttributeEvent srae) | 刪除属性时激发此方法 | |
attrbuteReplaced(ServletRequestAttributeEvent srae) | 修改属性时激发此方法 | |
ServletRequestListener | RequestDestroyed(ServletRequestAttributeEvent srae) | 销毁ServletRequest时激发此方法 |
RequestInitialized(ServletRequestAttributeEvent srae) | 创建ServletRequest时激发此方法 |
【案例6-5】通过监听器查看用户在线情况。
package ch6;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineListener implements HttpSessionListener{
private int onlineCount;//定义一个代表在线人数的变量
public OnlineListener(){onlineCount=0;}
public void sessionCreated(HttpSessionEvent sessionEvent) {//会话创建时的处理
onlineCount++;
sessionEvent.getSession().getServletContext().setAttribute("online",new Integer(onlineCount));
}
public void sessionDestroyed(HttpSessionEvent sessionEvent) {//会话销毁时的处理
onlineCount--;
sessionEvent.getSession().getServletContext().setAttribute("online",new Integer(onlineCount));
}
}
在web.xml文件中配置OnlineListener监听器,相关代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<!--声明过滤器-->
<listener>
<listener-class>ch6.OnlineListener</listener-class>
</listener>
</web-app>
创建JSP页面"online,jsp",测试OnlineListener监听器,相关代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>使用监听器监听在线人数的例子</title>
</head>
<body>
<center>
<h2>当前的在线人数:<%=(Integer)application.getAttribute("online") %></h2>
</center>
</body>
</html>
效果图如下图所示:


