Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。(通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发 sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。)
Servlet 监听器用于监听一些重要事件的发生,监听器对象在事情发生前、发生后可以做一些必要的处理。
目前 Servlet2.4 和 JSP2.0 常用的有7 个监听器接口,分为3 类:
1. Servlet上下文进行监听(Application级) :
用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:
(1) ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改
attributeAdded(ServletContextAttributeEvent e) 添加属性时调用
attributeReplaced(ServletContextAttributeEvent e) 修改属性时调用
attributeRemoved(ServletContextAttributeEvent e) 删除属性时调用
(2) ServletContextListener:监听对 ServletContext 对象的创建和删除
contextInitialized(ServletContextEvent sce) 初始化时调用
contextDestroyed(ServletContextEvent sce) 销毁时调用,即当服务器重新加载时调用
2. 监听HTTP会话(Session级) :
用于监听 HTTP 会话活动情况和 HTTP 会话中的属性设置情况,也可以监听 HTTP 会话的 active 和 passivate 情况等,该监听器需要用到如下多个接口类:
(1) HttpSessionListener:监听 HttpSession 的操作
sessionCreate(HttpSessionEvent se) 初始化时调用;
sessionDestroyed(httpSessionEvent se) 销毁时调用,即当用户注销时调用
在Web.xml的配置如下:
(2) HttpSessionActivationListener:用于监听 HTTP 会话的 active 和 passivate 情况
(3) HttpSessionAttributeListener:监听 HttpSession 中的属性操作
attributeAdded(HttpSessionBindingEvent se) 添加属性时调用
attributeRemoved(HttpSessionBindingEvent se) 删除属性时调用
attributeReplaced(HttpSessionBindingEvent se) 修改属性时调用
3. 对客户端请求进行监听(Requst级) :
用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:
(1) ServletRequestListener 接口类
requestDestroyed(ServletRequestEvent e) 对销毁客户端进行监听,即当执行 request.removeAttribute("xxx") 时调用
requestInitialized(ServletRequestEvent e) 对实现客户端的请求进行监听
在Web.xml中添加如下配置:
(2) ServletRequestAttributeListener 接口类
attributeAdded(ServletRequestAttributeEvent e) 对属性添加进行监听
attributeRemoved(ServletRequestAttributeEvent e) 对属性删除进行监听
attributeReplaced(ServletRequestAttributeEvent e) 对属性替换进行监听
参考资料: http://blog.youkuaiyun.com/yxl2005play/archive/2010/01/10/5170077.aspx
http://blog.youkuaiyun.com/opnmzxcvb/archive/2009/09/15/4554329.aspx
Servlet 监听器用于监听一些重要事件的发生,监听器对象在事情发生前、发生后可以做一些必要的处理。
目前 Servlet2.4 和 JSP2.0 常用的有7 个监听器接口,分为3 类:
1. Servlet上下文进行监听(Application级) :
用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:
(1) ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改
attributeAdded(ServletContextAttributeEvent e) 添加属性时调用
attributeReplaced(ServletContextAttributeEvent e) 修改属性时调用
attributeRemoved(ServletContextAttributeEvent e) 删除属性时调用
(2) ServletContextListener:监听对 ServletContext 对象的创建和删除
contextInitialized(ServletContextEvent sce) 初始化时调用
contextDestroyed(ServletContextEvent sce) 销毁时调用,即当服务器重新加载时调用
2. 监听HTTP会话(Session级) :
用于监听 HTTP 会话活动情况和 HTTP 会话中的属性设置情况,也可以监听 HTTP 会话的 active 和 passivate 情况等,该监听器需要用到如下多个接口类:
(1) HttpSessionListener:监听 HttpSession 的操作
sessionCreate(HttpSessionEvent se) 初始化时调用;
sessionDestroyed(httpSessionEvent se) 销毁时调用,即当用户注销时调用
- package com.wl.listener.test;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- public class HttpSessionListenerTest implements HttpSessionListener {
- public void sessionCreated(HttpSessionEvent arg0) {
- System.out.println("SSSSSSSSSSSSSSSSSS");
- }
- public void sessionDestroyed(HttpSessionEvent arg0) {
- System.out.println("EEEEEEEEEEEEEEEEEEE");
- }
- }
在Web.xml的配置如下:
- <listener>
- <listener-class>com.wl.listener.test.HttpSessionListenerTest</listener-class>
- </listener>
(2) HttpSessionActivationListener:用于监听 HTTP 会话的 active 和 passivate 情况
(3) HttpSessionAttributeListener:监听 HttpSession 中的属性操作
attributeAdded(HttpSessionBindingEvent se) 添加属性时调用
attributeRemoved(HttpSessionBindingEvent se) 删除属性时调用
attributeReplaced(HttpSessionBindingEvent se) 修改属性时调用
3. 对客户端请求进行监听(Requst级) :
用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:
(1) ServletRequestListener 接口类
requestDestroyed(ServletRequestEvent e) 对销毁客户端进行监听,即当执行 request.removeAttribute("xxx") 时调用
requestInitialized(ServletRequestEvent e) 对实现客户端的请求进行监听
- package com.wl.listener.test;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- public class ServletRequestListenerTest implements ServletRequestListener {
- public void requestDestroyed(ServletRequestEvent arg0) {
- System.out.println("ServletRequestListenerTest is destroyed .......");
- }
- public void requestInitialized(ServletRequestEvent arg0) {
- System.out.println("ServletRequestListenerTest is start .......");
- }
- }
在Web.xml中添加如下配置:
- <listener>
- <listener-class>com.wl.listener.test.ServletRequestListenerTest</listener-class>
- </listener>
(2) ServletRequestAttributeListener 接口类
attributeAdded(ServletRequestAttributeEvent e) 对属性添加进行监听
attributeRemoved(ServletRequestAttributeEvent e) 对属性删除进行监听
attributeReplaced(ServletRequestAttributeEvent e) 对属性替换进行监听
参考资料: http://blog.youkuaiyun.com/yxl2005play/archive/2010/01/10/5170077.aspx
http://blog.youkuaiyun.com/opnmzxcvb/archive/2009/09/15/4554329.aspx