下面小编就为大家带来一篇基于Listener监听器生命周期(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一、Listener生命周期
listener是web三大组件之一,是servlet监听器,用来监听请求,监听服务端的操作。
listener分为:(都是接口类,必须实现相应方法)
1.生命周期监听器(3个)
ServletContextListener requestDestroyed 在容器启动时被调用(在servlet被实例化前执行) requestInitialized 在容器销毁时调用(在servlet被销毁后执行) HttpSessionListener sessionCreated 在HttpSession创建后调用 sessionDestroyed 在HttpSession销毁前调用(执行session.invalidate();方法) ServletRequestListener requestDestroyed 在request对象创建后调用(发起请求) requestInitialized 在request对象销毁前调用(请求结束)
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) sessionDidActivate(HttpSessionEvent se)
二、测试范例
1.生命周期监听:
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextListener {<a style="color:transparent">来源gao($daima.com搞@代@#码网</a> /** * ServletContextListener实现方法 * @param sce */ public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContextListener初始化"); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContextListener销毁"); } }
其他两个监听器类似,不在重复贴出。
在web.xml中配置
<!-- 监听器 --><!-- servlet监听器 --> study.myListener.ServletContentAttribute_Listener<!-- session监听器 --> study.myListener.HttpSessionAttribute_Listener<!-- request监听器--> study.myListener.ServletRequestAttribute_Listener
运行结果:
2.属性监听:
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextAttributeListener{ /** * ServletContextAttributeListener实现方法 * @param event */ public void attributeAdded(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent添加属性:{0},属性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeRemoved(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent删除属性:{0},属性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeReplaced(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent替换属性:{0},属性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } }
另外两个监听器类似,不在赘诉。接下来用jsp页面测试
listenerDemo.jsp
<title>监听器设置</title>
执行结果如下:
注意:其中遇到一个问题:就是在启动tomcat的时候servletcontextListener监听执行了两次,最后删除掉server.xml中 Context 的手动配置,这样就不会加载两次了。
以上这篇基于Listener监听器生命周期(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网。
以上就是基于Listener监听器生命周期(详解)的详细内容,更多请关注gaodaima搞代码网其它相关文章!