监听器

JavaWeb监听器概述
JavaWeb三大组件都必须在Web.xml中配置
监听器: awt swing 事件监听 SAX 基于事件驱动 JS 事件 onclick btn.onlick = function () {} window.onload = function(){} text.onblur = function(){}
l 它是一个接口,内容由我们来实现;
l 它需要注册,需要注册在事件源上
l 监听器中的方法,会在特殊事件发生时被调用!

观察者:
l 事件源;
时间发生的对象
l 事件;
具体的事件行为
l 监听器;
注册在事件源的监听对象
在JavaWeb被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。
l 监听域对象“创建”与“销毁”的监听器; 生命周期监听器
l 监听域对象“操作域属性”的监听器; 属性监听器 监听 属性的 添加 删除 修改 application.setAttrbute("aaa", "AAA"); application.setAttrbute("aaa", "BBB"); application.removeAttrbute("aaa");
l 监听HttpSession的监听器。感知监听器 注册在JavaBean上面
创建与销毁监听器
创建与销毁监听器一共有三个:
l ServletContextListener:Tomcat启动和关闭时调用下面两个方法
public void contextInitialized(ServletContextEvent evt):ServletContext对象被创建后调用;
public void contextDestroyed(ServletContextEvent evt):ServletContext对象被销毁前调用;
l HttpSessionListener:开始会话和结束会话时调用下面两个方法
public void sessionCreated(HttpSessionEvent evt):HttpSession对象被创建后调用;
public void sessionDestroyed(HttpSessionEvent evt):HttpSession对象被销毁前调用;
l ServletRequestListener:开始请求和结束请求时调用下面两个方法
public void requestInitiallized(ServletRequestEvent evt):ServletRequest对象被创建后调用;
public void requestDestroyed(ServletRequestEvent evt):ServletRequest对象被销毁前调用。
事件对象
l ServletContextEvent:ServletContext getServletContext();
l HttpSeessionEvent:HttpSession getSession();
l ServletRequestEvent:
ServletRequest getServletRequest()
ServletContext getServletContext()

案例:
l 编写MyServletContextListener类,实现ServletContextListener接口;
l 在web.xml文件中部署监听器;
l 为了看到session销毁的效果,在web.xml文件中设置session失效时间为1分钟;

操作域属性的监听器
当对域属性进行增、删、改时,执行的监听器一共有三个:
l ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法。
public void attributeAdded(ServletContextAttributeEvent evt)
public void attributeRemoved(ServletContextAttributeEvent evt)
public void attributeReplaced(ServletContextAttributeEvent evt)
l HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法
public void attributeAdded(HttpSessionBindingEvent evt)
public void attributeRemoved (HttpSessionBindingEvent evt)
public void attributeReplaced (HttpSessionBindingEvent evt)
l ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法
public void attributeAdded(ServletRequestAttributeEvent evt)
public void attributeRemoved (ServletRequestAttributeEvent evt)
public void attributeReplaced (ServletRequestAttributeEvent evt)

下面对这三个监听器的事件对象功能进行介绍:
l ServletContextAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletContext getServletContext():获取ServletContext对象。
l HttpSessionBindingEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
HttpSession getSession():获取当前操作的session对象。
l ServletRequestAttributeEvent
String getName():获取当前操作的属性名;
Object getValue():获取当前操作的属性值;
ServletContext getServletContext():获取ServletContext对象;
ServletRequest getServletRequest():获取当前操作的ServletRequest对象。

HttpSession的监听器
还有两个与HttpSession相关的特殊的监听器,这两个监听器的特点如下:
l 不用在web.xml文件中部署;
l 这两个监听器不是给session添加,而是给Bean添加。即让Bean类实现监听器接口,然后再把Bean对象添加到session域中。

下面对这两个监听器介绍一下:
l HttpSessionBindingListener:当某个类实现了该接口后,可以感知本类对象添加到session中,以及感知从session中移除。例如让Person类实现HttpSessionBindingListener接口,那么当把Person对象添加到session中,或者把Person对象从session中移除时会调用下面两个方法:
public void valueBound(HttpSessionBindingEvent event):当把监听器对象添加到session中会调用监听器对象的本方法;
public void valueUnbound(HttpSessionBindingEvent event):当把监听器对象从session中移除时会调用监听器对象的本方法;

这里要注意,HttpSessionBindingListener监听器的使用与前面介绍的都不相同,当该监听器对象添加到session中,或把该监听器对象从session移除时会调用监听器中的方法。并且无需在web.xml文件中部署这个监听器。


l HttpSessionActivationListener:Tomcat会在session从时间不被使用时钝化session对象,所谓钝化session,就是把session通过序列化的方式保存到硬盘文件中。当用户再使用session时,Tomcat还会把钝化的对象再活化session,所谓活化就是把硬盘文件中的session在反序列化回内存。当session被Tomcat钝化时,session中存储的对象也被纯化,当session被活化时,也会把session中存储的对象活化。如果某个类实现了HttpSessionActiveationListener接口后,当对象随着session被钝化和活化时,下面两个方法就会被调用:
public void sessionWillPassivate(HttpSessionEvent se):当对象感知被活化时调用本方法;
public void sessionDidActivate(HttpSessionEvent se):当对象感知被钝化时调用本方法;

HttpSessionActivationListener监听器与HttpSessionBindingListener监听器相似,都是感知型的监听器,例如让Person类实现了HttpSessionActivationListener监听器接口,并把Person对象添加到了session中后,当Tomcat钝化session时,同时也会钝化session中的Person对象,这时Person对象就会感知到自己被钝化了,其实就是调用Person对象的sessionWillPassivate()方法。当用户再次使用session时,Tomcat会活化session,这时Person会感知到自己被活化,其实就是调用Person对象的sessionDidActivate()方法。
注意,因为钝化和活化session,其实就是使用序列化和反序列化技术把session从内存保存到硬盘,和把session从硬盘加载到内存。这说明如果Person类没有实现Serializable接口,那么当session钝化时就不会钝化Person,而是把Person从session中移除再钝化!这也说明session活化后,session中就不在有Person对象了。

原文地址:https://www.cnblogs.com/Nick7/p/10775418.html