Servlet2.5规范共有8中Listener接口,6种Event类型

ServletContextListener接口

[接口方法] contextInitialized()与 contextDestroyed()

[接收事件] ServletContextEvent

[触发场景] 在Container加载Web应用程序时(例如启动Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。



ServletContextAttributeListener

[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()

[接收事件] ServletContextAttributeEvent

[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。



HttpSessionListener

[接口方法] sessionCreated()与sessionDestroyed ()

[接收事件] HttpSessionEvent

[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。



HttpSessionAttributeListener

[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。



HttpSessionActivationListener

[接口方法] sessionDidActivate()与 sessionWillPassivate()

[接收事件] HttpSessionEvent

[触发场景]Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。



ServletRequestListener

[接口方法] requestInitialized()与 requestDestroyed()

[接收事件] RequestEvent

[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。



ServletRequestAttributeListener

[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。



HttpSessionBindingListener

[接口方法] valueBound()与valueUnbound()

[接收事件] HttpSessionBindingEvent

[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。

实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如

< listener>
      < listener-class >
             demo.servlet.listener.CustomServletContextListener
      < /listener-class >
< /listener>
public class ListenerTest implements HttpSessionListener{

      Log log = LogFactorygetLog(getClass());

      public void sessionCreated(HttpSessionEvent event){
             httpSession session =event.getSession();
             log.info("新建一个session,ID为"+session.getId());
      }

      public void sessionDestroyed(HttpSessionEvent event){
             httpSession session =event.getSession();
             log.info("销毁一个session,ID为"+session.getId());
      }
}

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

  1. 【Servlet】Java Serlvet Listener 监听器

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

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

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

  3. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  4. Servlet过滤器和监听器知识总结(转)

    Servlet过滤器和监听器知识总结(转)  Servlet过滤器是 Servlet 程序的一种特殊用法,主要用来完成一些通用的操作,如编码的过滤.判断用户的登录状态.过滤器使得Servlet开发者能 ...

  5. Listener监听器和Filter过滤器

    Listener监听器 WEB中的监听器 WEB 中的 Listener 和 Filter 是属于 Servlet 规范中的高级的技术.WEB中的监听器共有三类八种(监听三个域对象)* 事件源:Ser ...

  6. Servlet3.0的注解自定义原生Listener监听器实战

    简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...

  7. spring boot中使用servlet、listener和filter

    spring boot中支持使用java Web三大组件(servlet.listener和filter),但是坑比较多,主要是spring boot内嵌tomcat和独立tomcat服务器有一些细节 ...

  8. 15 Filter过滤器和Listener监听器

    1.Filter:过滤器 (1) 概念:生活中的过滤器:净水器,空气净化器,土匪.web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能.过滤器的作用:一般用于完成通用的 ...

  9. SpringBoot整合三大组建(Servlet、Listener、Filter)

    >[更多资源和教程请关注公众号:**非科班的科班**.如果觉得我写的还可以请给个赞,谢谢大家,你的鼓励是我创作的动力](https://blog.csdn.net/qq_43255017)## ...

随机推荐

  1. [Codeforces]860E Arkady and a Nobody-men

    屯一个虚树的板子,顺便总结一下这样的题型. Description 给定一棵n个节点的有根树,在输入数据通过给出每个节点的父亲来表示这棵树.若某个节点的父亲为0,那么该节点即为根.现在对于每个点,询问 ...

  2. java 实现WebService

    1.xml    2.    wsdl: webservice description language web服务描述语言        通过xml格式说明调用的地址方法如何调用,可以看错webse ...

  3. Linux学习之CentOS(十六)-----内存置换空间(swap)之建置(转)

    内存置换空间(swap)之建置 安装时一定需要的两个 partition 啰! 一个是根目录,另外一个就是 swap(内存置换空间), swap 的功能就是在应付物理内存不足的情况下所造成的内存延伸记 ...

  4. swift之属性

    知识点总结: 1.存储属性 struct Town{ let region = "South" //只读属性 var population = //读写属性 } 2.惰性存储属性 ...

  5. Mysql锁机制--读锁

    Mysql 系列文章主页 =============== 1 准备数据 1.1 建表 1.1.1 建立 Employee表 DROP TABLE IF EXISTS employee; CREATE ...

  6. Java8——快速入门手册(学习笔记)

    github博文传送门 Java8特性学习笔记 Java8中新增了许多的新特性,在这里本人研究学习了几个较为常用的特性,在这里与大家进行分享.(这里推荐深入理解Java 8用于理解基础知识)本文分为以 ...

  7. nginx 网络模型,cpu亲和等优点

    nginx优点1.IO多路复用epollIO多路复用:多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这里的"复用" 指的是复用同一个线程epollIO多路复用的实现方式 ...

  8. 如何改变hr颜色

    html中用css改变颜色,<hr style="border:0;background-color:#ff0000;height:1px;">如果不加border:0 ...

  9. iOS不能交互的几种情况

    alpha <=0.01 hidden = YES userInteraction = NO 父试图不允许交互,子试图也不允许交互: 在父试图可见范围内,可以交互,超出部分失效,不能交互

  10. R语言:安装及使用

    http://blog.csdn.net/pipisorry/article/details/53640638 ubuntu下安装 sudo apt-get install -y r-base源码安装 ...