Servlet Listener(器)
器的相关概念:
事件:方法调用、属性改变、状态改变等。
事件源:被的对象( 例如:request、session、servletContext)。
器:用于事件源对象 ,事件源对象状态的变化都会触发器。
注册器:将器与事件源进行绑定。
器的分类
Servlet 规范中定义了 8 个器接口,可以用于 ServletContext、HttpSession 和 ServletRequest 对象的生周期和属性变化事件。开发 Servlet 器需要实现相应的器接口并重写接口中的方法。器 Listener 按照的事件划分,可以分为 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 he) | 当 HttpSession 对象中新增一个属性时 |
public void attributeRemoved (HttpSessionBindingEvent he) | 当删除 HttpSession 对象中的一个属性时 | |||
public void attributeReplaced (HttpSessionBindingEvent he) | 当 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 种方式,分别是:
在 web.xml 中注册器;
使用 @WebListener 注册器。
1. 在 web.xml 中注册器
在 web.xml 中使用 <listener> 标签配置器,Web 容器会自动把器注册到事件源中,示例代码如下。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp网站站点" rel="nofollow" />
package net.biancheng.www.servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* 器实例代码
*
* @author 编程帮 www.biancheng网站站点" rel="nofollow" />
- 随机文章
- 核心危机(核心危机魔石合成攻略)
- 风儿(风儿轻轻的吹)
- 儿童教育文章(儿童教育)
- 光遇花手先祖位置(安卓光遇手花先祖)
- 广州4a广告公司(广州4a广告公司创意总监年薪)
- 抖音卡(抖音卡顿怎么解决)
- 兵马俑(兵马俑介绍和历史背景)
- 帆船比赛(帆船比赛视频)
- 海猫鸣泣之时游戏(海猫鸣泣之时游戏在哪玩)
- 韩国媳妇和小雪(韩国媳妇和小雪的父亲工资是多少)
- 儋州市第二中学(儋州市第二中学录取分数线)
- 和柳亚子(和柳亚子先生于田)
- 冰客(冰客果汁)
- yy魔兽(yy魔兽世界)
- 充值卡代理(充值卡代理加盟)
- 拆奶罩
- 郭妮小说(恶魔的法则郭妮小说)
- 东天目山(东天目山景区)
- 杭同(杭同培训中心怎么样)
- 蝙蝠给人类的一封信(蝙蝠给人类的一封信)
- 大松电饭煲(美的大松电饭煲)
- 服饰加盟(服饰加盟店招商)
- 疯狂填字(疯狂填字5)
- 观音普门品(观音普门品念诵全文)
- 河北省大运会(河北省大运会时间)
- 哈利波特官网(哈利波特官网在哪里)
- 骇客神条(骇客神条怎么辨别真假)
- 杜星霖(杜星霖图片)
- 查传倜(查传倜个人生活)
- 广州晓港公园(广州晓港公园正门图片)
