博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三、Listener介绍
阅读量:4074 次
发布时间:2019-05-25

本文共 2378 字,大约阅读时间需要 7 分钟。

使用Listener步骤:

- 定义Listenner实现类。
- 通过注解或在web.xml文件配置Listener。

3.1实现Listener类

常用Web事件监听器接口:

- ServletContextListener:用于监听Web应用的启动和关闭。
- ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变。
- ServletRequestListener:用于监听用户请求。
- ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变。
- HttpSessionListener:用于监听用户session的开始和结束。
- HttpSessionAttributeListener:用于监听HttpSession范围(session)内属性的改变。

ServletContextListener接口的方法:

- contextInitialized(ServletContextEvent sce):启动Web应用时,系统调用Listener的该方法。
- contextDestroyed(ServletContextEvent sce):关闭Web应用时,系统调用Listener的该方法。

@WebListenerpublic class GetConnListener implements ServletContextListener{
//应用启动时,该方法被电泳 public void contextInitialized(ServletContextEvent sce) { } //系统关闭时,调用该方法 public void contextDestroyed(ServletContextEvent sce) { }}

3.2配置Listener

配置Listener的三种方式:

- 使用@WebListener修饰Listener实现类即可。
- 在web.xml文档中使用

package.className

3.3使用ServletContextAttributeListener

ServletContextAttributeListener用于监听ServletContext(application)范围内属性的变化,实现该接口的方法需要实现如下三个方法:

- attributeAdded(ServletContextAttributeEvent event):当程序把一个属性存入application范围时触发该方法。
- attributeRemoved(ServletContextAttributeEvent event):当程序把一个属性从application范围删除时触发该方法。
- attributeReplaced(ServletContextAttributeEvent event):当程序替换application范围内的属性时将触发该方法。

3.4使用ServletRequestListener和ServletRequestAttributeListener

ServletRequestListener用于监听用户请求的到达,实现该接口的监听器需要实现如下两个方法:

- requestInitialized(ServletRequestEvent sre):用户请求到达、被初始化时触发该方法。
- requestDestroyed(ServletRequestEvent sre):用户请求结束、被销毁时触发该方法。

ServletRequestAttributeListener用于监听ServletRequest(request)范围内属性的变化。实现该接口的监听器需要实现attributeAdded()、attributeRemove()、attributeReplace()三个方法。

3.5使用HttpSessionListener和HttpSessionAtttributeListener

HttpSessionListener用于用户session的创建和销毁,实现该接口的监听器需要实现如下方法:

- sessionCreated(HttpSessionEvent se):用户与服务器的会话开始、创建时触发该方法。
- sessionDestroyed(HttpSessionEvent se):用户与服务器的会话断开、销毁时触发该方法。

HttpSessionAttributeListener则用于监听HttpSession(session)范围内属性的变化,实现该接口的监听器需要实现attributeAdded()、attributeRemoved()、attributeReplace()三个方法。

通过检查HttpServletRequest的做法可以更精确地监控在线用户的状态:

- 定义一个ServletRequestListener,这个监听器负责监听每个用户请求,当用户请求到达时,系统将用户的session ID、用户名、用户IP、正在访问的资源、访问时间记录下来。
- 启动一条后台线程,这条后台线程每隔一段时间检查上面的每条在线记录,如果某条在线记录的访问时间与当前时间相差超过了指定值,将这条在线记录删除即可。这条后台线程应随着Web应用的启动而启动,可考虑使用ServletContextListener来完成。

转载地址:http://sfkni.baihongyu.com/

你可能感兴趣的文章
《数据库系统概论》 第三章 关系数据库标准语言SQL
查看>>
SQL语句(二)查询语句
查看>>
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
IA32时钟周期的一些内容
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>