Servlet 中的 Listener
来源:互联网 发布:spark 大数据 编辑:程序博客网 时间:2024/05/17 08:30
Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
总体上说servlet中有主要有3类事件既:Servlet上下文事件、会话事件与请求事件总共有8个listener接口,我们在web.xml中注册时对应上自己对相应接口的实现类即可:
Servlet中的Listener和Event:
1. 在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。
ServletContextListener接口
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
例如负责加载/applicationContext.xml文件的spring的类ContextLoaderListener implements ServletContextListener 。
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中设定。
在jsp/servlet中具体的实现是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。具体使用方法:在web.xml中添加如下语句:
< listener >
< listener-class > com.servlet.listener.YouAchieveListener < /listener-class >
< /listener >
其中YouAchieveListener 为你实现的某个Listener接口的实现类com.servlet.listener.为你的包名。
- Servlet中的八大Listener
- Servlet中的Listener
- Servlet中的Listener
- servlet中的listener
- servlet中的listener
- Servlet 中的 Listener
- JSP/Servlet中的Listener
- Servlet 中的 Listener
- Servlet中的八大Listener
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet 中的 Listener 的应用
- Servlet中的Listener的应用
- 使用 Lambda Probe监控Tomcat
- Android XML解析学习——方式比较
- WinCE虚拟串口驱动
- 从今天开始...好好学习编程
- TextBox回车自动查询
- Servlet 中的 Listener
- tcp打洞
- AVL树
- souapp搜应用:Wing微博 新浪微博桌面客户端存在的安全问题你知道吗?
- dateAdd、dateDiff、DatePart参数比较参考
- Netbeans7.0横空出世
- asp.net 调试
- Apache JMeter web性能测试实例
- Windows mobile 使用codesnitch