咸鱼入门到放弃13--监听器(Listener)

一、监听器介绍

1.1、监听器的概念

  

  监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。监听器其实就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法立即被执行。

java的事件监听机制
1、事件监听涉及到三个组件:事件源、事件对象、事件监听器
2、当事件源上发生某一个动作时,它会调用事件监听器的一个方法,并在调用该方法时把事件对象传递进去,开发人员在监听器中通过事件对象,就可以拿到事件源,从而对事件源进行操作。

 

二、JavaWeb中的监听器

2.1、基本概念

  JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域对象的创建与销毁事件,以及监听这些域对象中的属性发生修改的事件。

2.2、Servlet监听器的分类

  在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为ServletContextHttpSessionServletRequest这三个域对象
  Servlet规范针对这三个对象上的操作,又把多种类型的监听器划分为三种类型:

  1. 监听域对象自身的创建和销毁的事件监听器。
  2. 监听域对象中的属性的增加和删除的事件监听器。
  3. 监听绑定到HttpSession域中的某个对象的状态的事件监听器。

2.3、监听ServletContext域对象的创建和销毁

  ServletContextListener接口用于监听ServletContext对象的创建和销毁事件。实现了ServletContextListener接口的类都可以对ServletContext对象的创建和销毁进行监听。

  当ServletContext对象被创建时,激发contextInitialized (ServletContextEvent sce)方法

  当ServletContext对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法

  ServletContext域对象创建和销毁时机:
    创建:服务器启动针对每一个Web应用创建ServletContext
    销毁:服务器关闭前先关闭代表每一个web应用的ServletContext

2.4、监听HttpSession域对象的创建和销毁

  HttpSessionListener 接口用于监听HttpSession对象的创建和销毁
  创建一个Session时,激发sessionCreated (HttpSessionEvent se) 方法
  销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se) 方法。

 

2.5、监听ServletRequest域对象的创建和销毁

  ServletRequestListener接口用于监听ServletRequest 对象的创建和销毁
  Request对象被创建时,监听器的requestInitialized(ServletRequestEvent sre)方法将会被调用
  Request对象被销毁时,监听器的requestDestroyed(ServletRequestEvent sre)方法将会被调用

  ServletRequest域对象创建和销毁时机:
    创建:用户每一次访问都会创建request对象
    销毁:当前访问结束,request对象就会销毁

编写监听器,代码如下:

复制代码
 1 package me.gacl.web.listener;
 2 
 3 import javax.servlet.ServletRequestEvent;
 4 import javax.servlet.ServletRequestListener;
 5 
 6 /**
 7 * @ClassName: MyServletRequestListener
 8 * @Description: MyServletRequestListener类实现了ServletRequestListener接口,
 9 *                 因此可以对ServletRequest对象的创建和销毁这两个动作进行监听。
10 * @author: 孤傲苍狼
11 * @date: 2014-9-9 下午11:50:08
12 *
13 */ 
14 public class MyServletRequestListener implements ServletRequestListener {
15 
16     @Override
17     public void requestDestroyed(ServletRequestEvent sre) {
18         System.out.println(sre.getServletRequest() + "销毁了!!");
19         
20     }
21 
22     @Override
23     public void requestInitialized(ServletRequestEvent sre) {
24         System.out.println(sre.getServletRequest() + "创建了!!");
25     }
26 }
复制代码

在web.xml文件中注册监听器

复制代码
1  <!--注册针对ServletRequest对象进行监听的监听器-->
2    <listener>
3       <description>ServletRequestListener监听器</description>
4       <listener-class>me.gacl.web.listener.MyServletRequestListener</listener-class>
5   </listener>
复制代码

Listener只需要注册不用映射。

三、监听域对象中属性的变更的监听器

  域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
  这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

3.1、attributeAdded 方法

  当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
  各个域属性监听器中的完整语法定义为:

1 public void attributeAdded(ServletContextAttributeEvent scae)
2 public void attributeReplaced(HttpSessionBindingEvent  hsbe)
3 public void attributeRmoved(ServletRequestAttributeEvent srae)

3.2、attributeRemoved 方法

  当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应
  各个域属性监听器中的完整语法定义为:

1 public void attributeRemoved(ServletContextAttributeEvent scae)
2 public void attributeRemoved (HttpSessionBindingEvent  hsbe)
3 public void attributeRemoved (ServletRequestAttributeEvent srae)

3.3、attributeReplaced 方法

  当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应
  各个域属性监听器中的完整语法定义为:

1 public void attributeReplaced(ServletContextAttributeEvent scae)
2 public void attributeReplaced (HttpSessionBindingEvent  hsbe)
3 public void attributeReplaced (ServletRequestAttributeEvent srae)

四、感知Session绑定的事件监听器

  保存在Session域中的对象可以有多种状态:

  • 绑定(session.setAttribute("bean",Object))到Session中;
  • 从 Session域中解除(session.removeAttribute("bean"))绑定;
  • 随Session对象持久化到一个存储设备中;
  • 随Session对象从一个存储设备中恢复

  Servlet 规范中定义了两个特殊的监听器接口"HttpSessionBindingListener和HttpSessionActivationListener"来帮助JavaBean 对象了解自己在Session域中的这些状态: ,实现这两个接口的类不需要 web.xml 文件中进行注册

1、HttpSessionBindingListener接口

  实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和 Session中删除的事件
  当对象被绑定到HttpSession对象中时,web服务器调用该对象的void valueBound(HttpSessionBindingEvent event)方法
  当对象从HttpSession对象中解除绑定时,web服务器调用该对象的void valueUnbound(HttpSessionBindingEvent event)方法

2、HttpSessionActivationListener接口

  实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件
  当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被钝化(序列化)之前,web服务器调用该javabean对象的void sessionWillPassivate(HttpSessionEvent event) 方法。这样javabean对象就可以知道自己将要和HttpSession对象一起被序列化(钝化)到硬盘中.
  当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被活化(反序列化)之后,web服务器调用该javabean对象的void sessionDidActive(HttpSessionEvent event)方法。这样javabean对象就可以知道自己将要和 HttpSession对象一起被反序列化(活化)回到内存中

 

 

https://www.cnblogs.com/xdp-gacl/p/3961929.html

https://www.cnblogs.com/xdp-gacl/p/3969249.html

posted @ 2019-04-12 17:15  lvoooop  阅读(286)  评论(0编辑  收藏  举报