Servlet中的八大Listener

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

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

其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。

1. Servlet中的Listener和Event:

在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。

l ServletContextListener

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

[接收事件] ServletContextEvent

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

l ServletContextAttributeListener

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

[接收事件] ServletContextAttributeEvent

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

l HttpSessionListener

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

[接收事件] HttpSessionEvent

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

l HttpSessionAttributeListener

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

[接收事件] HttpSessionBindingEvent

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

l ServletRequestListener

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

[接收事件] RequestEvent

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

l ServletRequestAttributeListener

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

[接收事件] HttpSessionBindingEvent

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

l HttpSessionBindingListener

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

[接收事件] HttpSessionBindingEvent

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

l HttpSessionActivationListener

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

[接收事件] HttpSessionEvent

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

2. 注册监听器

除了HttpSessionBindingListener和HttpSessionActivationListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如:

 

  1. <listener> 
  2.  
  3. <listener-class> 
  4.  
  5. demo.servlet.listener.CustomServletContextListener  
  6.  
  7. listener-class> 
  8.  
  9. listener> 
  10.  

 

【编辑推荐】

  1. J2ME手机RSS阅读器通过NetBeans 4.0也可创建
  2. 深入探究cookie技术在J2ME平台的应用与实现
  3. 探究J2ME中cookie库的管理
  4. MotorolaJ2ME开发时需要注意的几个细节
  5. Java2平台J2SE、J2EE、J2ME三大版本的区别
责任编辑:金贺 来源: ITEYE博客
相关推荐

2009-06-22 14:07:46

JSF优势

2010-04-19 11:23:26

Oracle开发工具

2012-05-10 16:45:54

linux系统

2023-12-27 11:45:09

2011-08-17 13:55:25

VoIPPBX

2022-01-05 09:26:56

IT灾难IT故障

2012-05-05 09:28:50

三星

2022-05-07 11:13:58

苹果汽车技术

2011-12-19 09:35:53

2011-04-14 18:03:49

2013-04-02 10:37:53

2023-06-07 16:21:16

2012-05-11 11:53:36

虚拟化

2011-07-11 14:01:12

JAVA

2020-06-28 14:01:50

漏洞管理漏洞攻击

2018-05-13 15:22:02

程序员职业陷阱

2009-11-04 14:30:22

2010-11-29 11:02:50

职场

2010-11-22 10:44:13

2015-01-19 14:56:53

SaaS应用云应用移动互联
点赞
收藏

51CTO技术栈公众号