Listener监听器生命周期
一、Listener生命周期
listener是web三大组件之一,是servlet监听器,用来监听请求,监听服务端的操作。
listener分为:(都是接口类,必须实现相应方法)
1.生命周期监听器(3个)
-
-
- ServletContextListener
- requestInitialized 在容器启动时被调用(在servlet被实例化前执行)
- requestDestroyed 在容器销毁时调用(在servlet被销毁后执行)
- HttpSessionListener
- sessionCreated 在HttpSession创建后调用
- sessionDestroyed 在HttpSession销毁前调用(执行session.invalidate();方法)
- ServletRequestListener
- requestDestroyed 在request对象创建后调用(发起请求)
- requestInitialized 在request对象销毁前调用(请求结束)
- ServletContextListener
-
2.属性变化监听器(3个)
-
-
-
- attributeAdded(ServletContextAttributeEvent event) 向appliction中添加属性时调用
- attributeRemoved(ServletContextAttributeEvent event) 从appliction中删除属性时调用
- attributeReplaced(ServletContextAttributeEvent event) 替换application中的属性时调用
- HttpSessionAttributeListener
- attributeAdded(HttpSessionBindingEvent event)
- attributeRemoved(HttpSessionBindingEvent event)
- attributeReplaced(HttpSessionBindingEvent event)
- ServletRequestAttributeListener
- attributeAdded(ServletRequestAttributeEvent event)
- attributeRemoved(ServletRequestAttributeEvent event)
- attributeReplaced(ServletRequestAttributeEvent event)
-
-
以上监听器接口除了传参不同,方法名都是一样的。分别监听application,session,request对象的属性变化。
3.session中指定类属性变化监听器(2)
-
-
- HttpSessionBindingListener
- valueBound(HttpSessionBindingEvent event) 当该类实例设置进session域中时调用
- valueUnbound(HttpSessionBindingEvent event) 当该类的实例从session域中移除时调用
- HttpSessionActivationListener
- sessionWillPassivate(HttpSessionEvent se) 当对象session被序列化(钝化)后调用
- sessionDidActivate(HttpSessionEvent se) 当对象session被反序列化(活化)后调用
- HttpSessionBindingListener
-
二、测试范例
1.生命周期监听:
ServletContentAttribute_Listener.java
1 public class ServletContentAttribute_Listener implements ServletContextListener { 2 /** 3 * ServletContextListener实现方法 4 * @param sce 5 */ 6 public void contextInitialized(ServletContextEvent sce) { 7 System.out.println("ServletContextListener初始化"); 8 } 9 10 public void contextDestroyed(ServletContextEvent sce) { 11 System.out.println("ServletContextListener销毁"); 12 } 13 }
其他两个监听器类似,不在重复贴出。
在web.xml中配置
1 <!-- 监听器 --> 2 <!-- servlet监听器 --> 3 <listener> 4 <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class> 5 </listener> 6 7 <!-- session监听器 --> 8 <listener> 9 <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class> 10 </listener> 11 12 <!-- request监听器--> 13 <listener> 14 <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class> 15 </listener>
运行结果:
2.属性监听:
ServletContentAttribute_Listener.java
1 public class ServletContentAttribute_Listener implements ServletContextAttributeListener{ 2 3 /** 4 * ServletContextAttributeListener实现方法 5 * @param event 6 */ 7 public void attributeAdded(ServletContextAttributeEvent event) { 8 String meg = MessageFormat.format("ServletContent添加属性:{0},属性值:{1}",event.getName(),event.getValue()); 9 System.out.println(meg); 10 } 11 12 public void attributeRemoved(ServletContextAttributeEvent event) { 13 String meg = MessageFormat.format("ServletContent删除属性:{0},属性值:{1}",event.getName(),event.getValue()); 14 System.out.println(meg); 15 } 16 17 public void attributeReplaced(ServletContextAttributeEvent event) { 18 String meg = MessageFormat.format("ServletContent替换属性:{0},属性值:{1}",event.getName(),event.getValue()); 19 System.out.println(meg); 20 } 21 22 }
另外两个监听器类似,不在赘诉。接下来用jsp页面测试
listenerDemo.jsp
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2017/10/17 5 Time: 15:28 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>监听器设置</title> 12 </head> 13 <body> 14 <% 15 /** 16 * servlet监听 17 */ 18 application.setAttribute("name","changxiang"); 19 application.setAttribute("name","小Cai先森"); 20 application.removeAttribute("name"); 21 22 /** 23 * session监听 24 */ 25 session.setAttribute("sessionName","changxiang"); 26 session.setAttribute("sessionName","小Cai先森"); 27 session.removeAttribute("sessionName"); 28 session.invalidate(); 29 /** 30 * request监听 31 */ 32 request.setAttribute("requestName","changxiang"); 33 request.setAttribute("requestName","小Cai先森"); 34 request.removeAttribute("requestName"); 35 %> 36 </body> 37 </html>
执行结果如下:
注意:其中遇到一个问题:就是在启动tomcat的时候servletcontextListener监听执行了两次,最后删除掉server.xml中 Context 的手动配置,这样就不会加载两次了。
解决思路:http://blog.csdn.net/shaokai132333/article/details/53328258