监听器 Listener 是一个实现特定接口的 Java 程序,这个程序专门用于监听另一个 Java 对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即自动执行。

监听器的相关概念:

  • 事件:方法调用、属性改变、状态改变等。
  • 事件源:被监听的对象( 例如:request、session、servletContext)。
  • 监听器:用于监听事件源对象 ,事件源对象状态的变化都会触发监听器。
  • 注册监听器:将监听器与事件源进行绑定。

监听器的分类

Servlet 规范中定义了 8 个监听器接口,可以用于监听 ServletContext、HttpSession 和 ServletRequest 对象的生命周期和属性变化事件。开发 Servlet 监听器需要实现相应的监听器接口并重写接口中的方法。

监听器 Listener 按照监听的事件划分,可以分为 3 类:

  1. 监听对象创建和销毁的监听器
  2. 监听对象中属性变更的监听器
  3. 监听 HttpSession 中的对象状态改变的监听器

监听对象创建和销毁的监听器

Servlet 规范定义了监听 ServletContext、HttpSession、HttpServletRequest 这三个对象创建和销毁事件的监听器,如下表所示。

事件源 监听器 监听器描述 创建和销毁方法 调用时机
ServletContext ServletContextListener 用于监听 ServletContext 对象的创建与销毁过程 void contextInitialized (ServletContextEvent sce) 当创建 ServletContext 对象时
void contextDestroyed (ServletContextEvent sce) 当销毁 ServletContext 对象时
HttpSession HttpSessionListener 用于监听 HttpSession 对象的创建和销毁过程 void sessionCreated (HttpSessionEvent se) 当创建 HttpSession 对象时
void sessionDestroyed (HttpSessionEvent se) 当销毁 HttpSession 对象时
ServletRequest ServletRequestListener 用于监听 ServletRequest 对象的创建和销毁过程 void requestInitialized (ServletRequestEvent sre) 当创建 ServletRequest 对象时
void requestDestroyed (ServletRequestEvent sre) 当销毁 ServletRequest 对象时

监听属性变更的监听器

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

事件源 监听器 监听器描述 方法 调用时机
ServletContext ServletContextAttributeListener 用于监听 ServletContext 对象的属性新增、移除和替换 public void attributeAdded (ServletContextAttributeEvent scae)  当 ServletContext 对象中新增一个属性时
public void attributeRemoved (ServletContextAttributeEvent scae)  当删除 ServletContext 对象中的一个属性时
public void attributeReplaced (ServletContextAttributeEvent scae)  当 ServletContext 对象中的某个属性被替换时
HttpSession HttpSessionAttributeListener 用于监听 HttpSession 对象的属性新增、移除和替换 public void attributeAdded  (HttpSessionBindingEvent  hsbe)  当 HttpSession 对象中新增一个属性时
public void attributeRemoved (HttpSessionBindingEvent  hsbe) 当删除 HttpSession 对象中的一个属性时
public void attributeReplaced (HttpSessionBindingEvent  hsbe)  当 HttpSession 对象中的某个属性被替换时
HttpServletRequest ServletRequestAttributeListener 用于监听 HttpServletRequest 对象的属性新增、移除和替换 public void attributeAdded (ServletRequestAttributeEvent srae) 当 HttpServletRequest 对象中新增一个属性时
public void attributeRemoved (ServletRequestAttributeEvent srae) 当删除 HttpServletRequest 对象中的一个属性时
public void attributeReplaced (ServletRequestAttributeEvent srae) 当 HttpServletRequest 对象中的某个属性被替换时

监听 Session 中对象状态改变的监听器

Session 中的对象可以有多种状态:绑定到 Session 中、从 Session 中解除绑定、随 Session 对象持久化到存储设备中(钝化)、随 Session 对象从存储设备中恢复(活化)。

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

事件源 监听器 监听器描述 方法 调用时机
HttpSession HttpSessionBindingListener 用于监听 JavaBean 对象绑定到 HttpSession 对象和从 HttpSession 对象解绑的事件 void  valueBound (HttpSessionBindingEvent event) 当对象被绑定(添加)到 HttpSession 对象中时
void  valueUnbound (HttpSessionBindingEvent event) 当对象从 HttpSession 对象中解除绑定(移除)时
HttpSessionActivationListener 用于监听 HttpSession 中对象活化和钝化的过程 void sessionWillPassivate (HttpSessionBindingEvent event) 当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被钝化之前
void  sessionDidActive (HttpSessionBindingEvent event) 当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被活化之后

注册监听器

注册 Servlet 监听器有 2 种方式,分别是:

  1. 在 web.xml 中注册监听器;
  2. 使用 @WebListener 注册监听器。

1. 在 web.xml 中注册监听器

在 web.xml 中使用 <listener> 标签配置监听器,Web 容器会自动把监听器注册到事件源中,示例代码如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>net.biancheng.www.listener.MySessionListener</listener-class>
</listener>
</web-app>

2. 使用 @WebListener 注解注册监听器

在监听器类上使用 @WebListener 注解,可以将该 Java 类注册为一个监听器类。示例代码如下。

package net.biancheng.www.servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 监听器实例代码
*
* @author 编程帮 www.biancheng.net
*/
@WebListener
public class MyListener implements ServletContextListener {
public MyListener() {
}
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
}
}

使用 HttpSessionBindingListener 和 HttpSessionActivationListener 时,不必进行注册,直接创建 Java 类实现这两个接口即可。

Servlet Listener(监听器)的更多相关文章

  1. Servlet之监听器(Listener)

    一.监听器(Listener)概述 1.概念 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 Se ...

  2. 【Servlet】Java Serlvet Listener 监听器

    Listener监听器 Servlet规范中定义的一种特殊的组件,用来监听Servlet容器产生的事件并进行相应的处理 容器产生的事件分类 - 生命周期相关的事件 - 设置和删除Attribute数据 ...

  3. Servlet的监听器Listener

    Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是 随web应用的启动而启动,只初始化一次,随web ...

  4. Servlet之Listener监听器

    Servlet2.5规范共有8中Listener接口,6种Event类型 ServletContextListener接口 [接口方法] contextInitialized()与 contextDe ...

  5. [Java] JSP笔记 - Listener 监听器

    Java Web 开发时,可以使用 Listener 来监听来监听一些事件,从而实现一些功能.实际上这个监听器,原理就是 Delphi 中大家常用的各种事件. 1. 那么,监听器的主要用途用哪些呢: ...

  6. listener监听器

    前言:之前写了一篇关于Filter的文章:http://tianweili.github.io/blog/2015/01/26/java-filter/,现在再来一篇Listener的,Filter和 ...

  7. spring listener监听器

    1.Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listen ...

  8. j2ee servlet listener

    JSP/Servlet 中的事件处理写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响 ...

  9. Listener 监听器

    Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listener ...

  10. Servlet, Listener 、 Filter.

    Java Web的三大组件:Servlet, Listener . Filter. 使用Listener监听器:八大监听器: 第一组:用于监听Servlet三个域对象的创建与销毁 1. Servlet ...

随机推荐

  1. ACwing1211. 蚂蚁感冒

    题目: 长 100 厘米的细长直杆子上有 n 只蚂蚁. 它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是 1 厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁 ...

  2. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  3. 【剑指Offer】06. 从尾到头打印链表 解题报告(Java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈 递归 数组 日期 题目地址:https://leetcode ...

  4. 【LeetCode】482. License Key Formatting 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】284. Peeking Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/peeking-i ...

  6. 1266 - Points in Rectangle

    1266 - Points in Rectangle    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  7. hdu-3183A Magic Lamp(贪心)

    题目的意思是: 给你一个大数,然后删减其中的K个数,并且剩下的数还是按原来在的先后次序排列,求所得的那个数最小的那个数. 思路:贪心(要取得数最小,你从左往右选数的时候,选的第一数,就是选后组成数的位 ...

  8. RabbitMQ学习笔记五:RabbitMQ之优先级消息队列

    RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...

  9. jsp:useBean的scope属性

    目录 1.分类 2.page 3.request 4.session 5.application 1.分类 对于jsp:useBean创建的对象,由jsp:useBean的可选属性scope指定,可以 ...

  10. GeoWave实践2:使用命令行将本地矢量数据注入hbase并发布至GeoServer

    目录 步骤 补充 步骤 在GeoWave所在的节点打开两个会话窗口. 会话1 //运行GeoServer,默认端口8080 geowave gs run 会话2 //为GeoWave创建数据库Test ...