读书人

Servlet中的Listener跟Event

发布时间: 2012-09-16 17:33:16 作者: rapoo

Servlet中的Listener和Event
Servlet 中的 Listener 和 Event:

1. 在 JSP 2.0/Servlet 2.4 中,共有八个 Listener 接口,六个 Event 类别。
ServletContextListener 接口
[ 接口方法 ] contextInitialized() 与 contextDestroyed()
[ 接收事件 ] ServletContextEvent
[ 触发场景 ] 在 Container 加载 Web 应用程序时(例如启动 Container 之后),会呼叫 contextInitialized() ,而当容器移除 Web 应用程序时,会呼叫 contextDestroyed () 方法。

2. ServletContextAttributeListener
[ 接口方法 ] attributeAdded() 、 attributeReplaced() 、 attributeRemoved()
[ 接收事件 ] ServletContextAttributeEvent
[ 触发场景 ] 若有对象加入为 application ( ServletContext )对象的属性,则会呼叫 attributeAdded() ,同理在置换属性与移除属性时,会分别呼叫 attributeReplaced() 、 attributeRemoved() 。

3. HttpSessionListener
[ 接口方法 ] sessionCreated() 与 sessionDestroyed ()
[ 接收事件 ] HttpSessionEvent
[ 触发场景 ] 在 session ( HttpSession )对象建立或被消灭时,会分别呼叫这两个方法。

4. HttpSessionAttributeListener
[ 接口方法 ] attributeAdded() 、 attributeReplaced() 、 attributeRemoved()
[ 接收事件 ] HttpSessionBindingEvent
[ 触发场景 ] 若有对象加入为 session ( HttpSession )对象的属性,则会呼叫 attributeAdded() ,同理在置换属性与移除属性时,会分别呼叫 attributeReplaced() 、 attributeRemoved() 。

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

6. ServletRequestListener
[ 接口方法 ] requestInitialized() 与 requestDestroyed()
[ 接收事件 ] RequestEvent
[ 触发场景 ] 在 request ( HttpServletRequest )对象建立或被消灭时,会分别呼叫这两个方法。

7. ServletRequestAttributeListener
[ 接口方法 ] attributeAdded() 、 attributeReplaced() 、 attributeRemoved()
[ 接收事件 ] HttpSessionBindingEvent
[ 触发场景 ] 若有对象加入为 request ( HttpServletRequest )对象的属性,则会呼叫 attributeAdded() ,同理在置换属性与移除属性时,会分别呼叫 attributeReplaced() 、 attributeRemoved() 。

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

读书人网 >软件架构设计

热点推荐