Servlet API中的八种监听器接口
来源:互联网 发布:大数据双创中心是什么 编辑:程序博客网 时间:2024/05/01 15:04
通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应。
一、Servlet中的Listener和Event:共有八个Listener接口,六个Event类别。
1、ServletContextListener接口
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
2、ServletContextAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletContextAttributeEvent
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
3、ServletRequestListener
[接口方法] requestInitialized()与 requestDestroyed()
[接收事件] ServletRequestEvent
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
4、ServletRequestAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletRequestAttributeEvent
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
5、HttpSessionListener
[接口方法] sessionCreated()与sessionDestroyed ()
[接收事件] HttpSessionEvent
[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
6、HttpSessionActivationListener
[接口方法] sessionDidActivate()与 sessionWillPassivate()
[接收事件] HttpSessionEvent
[触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透 过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容 易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。
7、HttpSessionAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
8、HttpSessionBindingListener
[接口方法] valueBound()与valueUnbound()
[接收事件] HttpSessionBindingEvent
[触发场景] 实现HttpSessionBindingListener接 口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现 HttpSessionBindingListener接口的类别不需在web.xml中设定。
二、 如何注册Servlet中的事件
实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如:
<listener> <listener-class>com.zmj.web.listener.ContextListener</listener-class> </listener>
- Servlet API中的八种监听器接口
- Servlet 监听器接口
- Servlet中的监听器
- servlet中的监听器
- servlet中的监听器深入
- Servlet中的监听器简介
- Servlet--Servlet进阶API、过滤器、监听器
- JavaWeb-监听器、Servlet规范中的监听器
- Servlet监听器中的一些使用方法
- Servlet监听器在开发中的应用案例
- 5-1 Servlet中的监听器、过滤器、JNDI
- servlet监听器如何调用Springside4中的Bean
- Servlet监听器在开发中的应用案例
- Servlet的三种监听器
- Servlet监听器
- servlet监听器
- Servlet监听器
- Servlet监听器
- 模板方法模式——以不变应万变
- NYOJ:奇怪的排序
- poj 2420
- css3 盒子类型
- Python |绝不乱入的靠谱书单
- Servlet API中的八种监听器接口
- ios 抛物线添加到购物车
- 树状数组( 单点修改/区间修改+区间求和+一维/二维)
- 191. Number of 1 Bits *
- Memcached 之 .NET(C#)实例分析
- 在Ubuntu14.04中安装Docker
- Hadoop伪分布式环境搭建
- <设计模式12>组合模式
- Java 两个变量的值互相交换