1.监听器简介:

监听器主要用来监听对象的创建,属性的变化,是一个实现特定接口的普通Java类。

Listener接口与事件对应表:

ServletContext

有关

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSession

有关

HttpSessionListener

HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener

HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequest

有关

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

编写监听器的步骤:

编写实现类->在web.xml中进行部署->编写测试页面

2.与ServletContext相关监听器

单个Web站点的资源都共享一个javax.servlet.ServletContext类的实体。通过该对象可以存取应用程序的全局对象以及初始化阶段的变量全局对象即为Application范围对象,其生命周期从容器启动至容器关闭。初始阶段的变量是指在web.xml中,由<context-param>元素设定的变量,该变量的范围是Application范围

ServletContextListener接口:

实现了该接口的程序,当JavaWeb应用程序启动时,会自动开始监听工作

首先调用contextInitialized()方法接收对应的ServletContextEvent事件

当应用从容器中移除时,会自动调用contextDestroyed()方法

以上两个方法都会接收到ServletContextEvent事件对象,该对象可以调用getServletContext()方法取得ServletContext对象(全局对象)

ServletContextAttributeListener接口:

实现该接口的程序,能够监听ServletContext属性的变化,例如:当往ServletContext中添加数据时,该程序会被调用。

方法

说明

attributeAdded(ServletContextAttributeEvent  e)

若有对象加入Application范围时,通知

正在收听的对象

attributeReplaced(ServletContextAttributeEvent  e)

若在Application的范围,有对象取代另

一个对象,通知正在收听的对象

attributeRemoved(ServletContextAttributeEvent  e)

若有对象从Application范围移出时,通

知正在收听的对象

ServletContextAttributeEvent的主要方法:getName(),getValue();attributeReplaced()方法中,getName()与getValue()是取之前的值。

3.与HttpSession相关监听器:

HttpSessionListener:

HttpSessionListener监听Session对象的创建与销毁,当有Session对象产生或销毁时,会自动调用sessionCreated()或sessionDestroyed()两个方法。

HttpSessionEvent事件:

HttpSessionListener接口与HttpSessionActivationListener接口都使用HttpSessionEvent事件对象

HttpSessionEvent类主要的方法:

getSession()

HttpSessionActivationListener接口:

该接口主要用于:同一个Session转移到不同JVM的情形(如:负载均衡,这些JVM可以在同一台机器或分散在网络中的多台机器)

当Session被储存起来,并且等待转移至另一个JVM,这段时间称为失效状态(Passivate),若Session中的属性对象实现HttpSessionActivationListener接口时,Container会自动调用sessionWillPassivate()方法通知该对象的Session已变成失效状态

当Session被转移至其他JVM之后,它又成为有效状态(Activate),此时Container会自动调用sessionDidActivate()方法通知该对象的Session已变成有效状态。

方法

说明

HttpSessionListener接口

sessionCreated(HttpSessionEvent e)

通知正在收听的对象,Session已经被加

载及初始化

sessionDestroyed(HttpSessionEvent e)

通知正在收听的对象, Session已经被

载出

HttpSessionActivationListener接口

sessionDidActivate(HttpSessionEvent e)

通知正在收听的对象,它的Session已经

被变为有效状态

sessionWillPassivate(HttpSessionEvent e)

通知正在收听的对象,它的Session已经

被变为无效状态

HttpSessionAttributeListener:

HttpSessionAttributeListener会监听Session属性的变化,功能与ServletContextAttributeListener接口类似,包含三个方法

attributeAdded()

attributeReplaced()

attributeRemove()

HttpSessionBindingEvent事件

HttpSessionBindingEvent事件主要有三个方法

getName()

getSession()

getValue()

HttpSessionBindingListener:

实现HttpSessionBindingListener接口的对象加入Session范围或从Session范围中移除时,容器会分别自动调用下面两个方法:

valueBound(HttpSessionBindingEvent e)

valueUnbound(HttpSessionBindingEvent e)

HttpSessionBindingListener接口是唯一不需要在web.xml中设定的Listener

自定义实现HttpSessionBindingListener接口的类

实例化监听器类的对象

将该对象添加到Session中

HttpSessionAttributeListener

HttpSessionAttributeListener使用的事件与HttpSessionBindingListener使用的事件相同: HttpSessionBindingEvent

HttpSessionAttributeListener与HttpSessionBindingListener的不同在于:

前者监听Web站点所有Session范围的变化

后者只监听Session范围内实现了HttpSessionBindingListener接口的对象移入移出

方法

说明

HttpSessionBindingListener接口

valueBound(HttpSessionBindingEvent  e)

当实现

HttpSessionBindingListener的对

象加入session时,会调用该方法

valueUnbound(HttpSessionBindingEvent  e)

当实现

HttpSessionBindingListener的对

象在session中销毁时,调用该方

HttpSessionAttributeListener接口

attributeAdded(HttpSessionBindingEvent  e)

若有对象加入Session范围时,通

知正在收听的对象

attributeReplaced(HttpSessionBindingEvent  e)

若在Session的范围,有对象取代

另一个对象,通知正在收听的对

attributeRemoved(HttpSessionBindingEvent  e)

若有对象从Session范围移出时,

4通知正在收听的对象

4.与ServletRequest相关监听器:

ServletRequestListener接口:

当有请求产生或销毁,会自动调用该接口实现的requestInitialized()或requestDestroyed()方法

该接口使用ServletRequestEvent事件

方法

说明

requestInitialized(ServletRequestEvent  e)

通知正在收听的对象,

ServletRequest已经被加载及初始

requestDestroyed(ServletRequestEvent  e)

通知正在收听的对象,

ServletRequest已经被载出

ServletRequestEvent的主要方法:

getServletContext()

getServletRequest()

ServletResquestAttributeListener

该接口监听Request范围的变化,有三个主要方法:

attributeAdded()

attributeReplaced()

attributeRemoved()

使用ServletRequestAttributeEvent事件。

ServletRequestAttributeEvent主要方法

getName()

getValue()

监听器的应用:

ServletContext范围的监听器可以进行一些初始化的动作,如:当Web应用启动的时候进行全局配置

Session范围的监听器对一个会话过程(与客户端关联)中所产生的事件进行响应,可以对客户端信息的变化进行跟踪

Request范围的监听器可以监听用户的每次请求

实现了该接口的程序,当JavaWeb应用程序启动时,会自动开始监听工作首先调用contextInitialized()方法接收对应的ServletContextEvent事件当应用从容器中移除时,会自动调用contextDestroyed()方法以上两个方法都会接收到ServletContextEvent事件对象,该对象可以调用getServletContext()方法取得ServletContext对象(全局对象)

5.监听器(Listener)相关推荐

  1. java web自定义监听器_Android自定义监听器Listener(自定义Java Callback回调事件)

    Callback回调事件介绍 Java或Android中创建异步回调最普遍的做法就是使用listener监听器或者observer观察者模式来解决,listener回调事件通常用于实现一个代码去监听另 ...

  2. 一篇文章教你学会使用SpringBatch 监听器Listener

    文章目录 一.SpringBatch监听器 二.搭建SpringBatch开发环境 三.监听器详细介绍 1.JobExecutionListener 2.StepExecutionListener 3 ...

  3. 大数据WEB阶段(十六)JavaEE三大 核心技术之监听器Listener

    Listener监听器 一.概述 Servlet三大核心技术之一 Servlet.Filter.Listener Servlet技术规范中定义了八种监听器用来监听web应用开发中对应的事件. 监听器可 ...

  4. JavaWeb中监听器Listener+过滤器filter+拦截器interceptor区别

    JavaWeb中监听器Listener+过滤器filter+拦截器interceptor区别 如果从整个项目中看,一个servlet请求的执行过程就变成了这样context-param–>lis ...

  5. 监听器Listener

    监听器Listener Servlet监听器是监听事件发生,在事件发生前后能够做出相应处理的web应用组件. PS:与传统监听直接将监听事件注册在事件源上不同,Servlet的监听器是统一配置在web ...

  6. Java监听器Listener使用说明

    转载:http://blog.csdn.net/meng2602956882/article/details/13511587 1.什么是Java监听器 监听器也叫Listener,是Servlet的 ...

  7. SpringBoot定义三大组件Servlet,过滤器Filter,监听器Listener

    SpringBoot定义三大组件Servlet,过滤器Filter,监听器Listener 1.定义组件的配置类: com.example.mybatis2018.config.MyServletCo ...

  8. servlet监听器Listener介绍和使用

    1监听器Listener 1.1 Listener简介 javaEE包括13门规范 在课程中主要学习 servlet技术 和 jsp技术,其中 servlet规范包括三个技术点:servlet,lis ...

  9. httplistener java_Java监听器listener的介绍

    Java监听器listener的介绍 listener 能做什么 当web中某些动作发生之后,服务器就调用listener中对应的方法. 内部机制 接口回调 Web监听器 步骤 创建需要的监听器类,实 ...

  10. 利用监听器(Listener)实现用户访问记录

    自定义监听器(Listener) 1. 当用户访问网站时,先判断用户的session是否为new: 2. 如果该session为new,获取用户的真实IP,查询数据库对应的今天访问信息是否有该IP: ...

最新文章

  1. matlab 十六进制数组,【MATLAB】MATLAB中读取二进制数据文件并加入到矩阵中
  2. if语句的一个错误记录,多了个“;”号
  3. 仿微信未读RecyclerView平滑滚动定位效果
  4. MyEclipse导入新项目后,不能发布到Tomcat
  5. 中的 终端报错怎么看原因_《琅琊榜》中太子被封禁,高湛引皇上看桂花的真正原因是什么?...
  6. 不等号属于不等式吗_考研专业课备考时,仅仅多刷几遍目标院校的期末考试题就够吗?...
  7. vim关于python的自动补全插件
  8. appium 切换native/ webview,findby,还有页面元素定位一直小于0的问题的解决
  9. cmake 多次编译_Part01_CMakeLists构建管理多个模块的C代码
  10. pb 如何判断数据窗口中是否有某个字段_怎么判断数据窗口中某个字段存在重复的数据...
  11. php -- 取日期
  12. 性能优化篇 之 如何开展优化类的工作(1)
  13. 基于Python的卷积神经网络和特征提取(Theano)
  14. EPON联通网关超级管理员密码获取办法
  15. setup maven plugin connection
  16. awvs安装|User acunetix already exist. Home directory for user acunetix not found.
  17. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
  18. SAP UI5 应用开发教程之一百 - 如何修改 SAP UI5 框架的源代码实现,以及使用本地部署的 SAP UI5 SDK 试读版
  19. oracle 用户名密码找回
  20. eureka核心知识梳理

热门文章

  1. BasicAuth认证实现
  2. Linux下安装Git
  3. VCard SaaS v7.3.1 - 数字名片生成器 SaaS - Laravel VCard SaaS
  4. 用完成端口开发大响应规模的Winsock应用程序(4)
  5. arduino红外接受
  6. 2021年安全月宣教用品
  7. linux如何退出,Linux退出命令为初学者解释(附例)
  8. Windows XP十五周年:由爱生恨的系统长者
  9. 奇怪的自助餐厅--扫描线
  10. lxc共享usb设备