Servlet的llistener接口详解
来源:互联网 发布:mac pro apple care 编辑:程序博客网 时间:2024/05/20 15:10
总体上说 servlet 中有主要有 3 类事件既: Servlet 上下文事件、会话事件与请求事件总共有 8 个 listener( 部分类容转载于 http://ritaleo.javaeye.com/blog/48751 ) 接口,我们在 web.xml 中注册时对应上自己对相应接口的实现类即可:
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 中设定。
具体使用方法:在 web.xml 中添加如下语句:
< listener >
< listener -class > com.servlet .listener .YouAchieveListener < /listener -class >
< /listener >
其中 YouAchieveListener 为你实现的某个 Listener 接口的实现类 com.servlet .listener . 为你的包名。
- Servlet的llistener接口详解
- Servlet接口方法详解
- Servlet详解---Servlet的生命周期
- Servlet 3.0的AsyncListener接口
- Servlet接口的常用方法
- Servlet的配置文件详解
- servlet&jsp的详解
- Servlet的url详解
- servlet的request详解
- 详解servlet的生命周期
- Servlet ---- 接口
- Servlet接口
- Servlet接口
- Servlet------接口
- Servlet(接口)
- Servlet接口
- Servlet接口
- Servlet--Servlet接口
- maven-pom中配置多个远程仓库-编码gbk的不可映射字符
- idea中maven依赖图
- Java NIO和IO的区别
- 安卓HAL层 so库文件加载原理
- 推荐!手把手教你使用Git
- Servlet的llistener接口详解
- 锐捷面试_guozhijun20160704
- BZOJ4636 蒟蒻的数列
- ubuntu及linuxmint下电子词典首选:GoldenDict
- Struts小总结
- eclipse超级实用快捷键
- 我的实习(第一天)
- EventBus源码分析(三):post方法发布事件【获取事件的所有订阅者,反射调用订阅者事件处理方法】(2.4版本)
- 调用startActivityForResult,onActivityResult无响应的问题