在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Servlet之Listener监听器

2016-12-20 13:12| 发布者: zhangjf| 查看: 425| 评论: 0

摘要: Servlet2.5规范共有8中Listener接口,6种Event类型 ServletContextListener接口 contextInitialized()与 contextDestroyed() ServletContextEvent 在Container加载Web应用程序时(例如启动Container之后), ...


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中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如

  1. < listener>
  2. < listener-class >
  3. demo.servlet.listener.CustomServletContextListener
  4. < /listener-class >
  5. < /listener>
复制代码

  1. public class ListenerTest implements HttpSessionListener{
  2. Log log = LogFactorygetLog(getClass());
  3. public void sessionCreated(HttpSessionEvent event){
  4. httpSession session =event.getSession();
  5. log.info("新建一个session,ID为"+session.getId());
  6. }
  7. public void sessionDestroyed(HttpSessionEvent event){
  8. httpSession session =event.getSession();
  9. log.info("销毁一个session,ID为"+session.getId());
  10. }
  11. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 04:18

Copyright 2015-2025 djqfx

返回顶部