当前位置: 首页 > news >正文

JAVA监听器(学习自用)

一、什么是监听器

servlet监听器是一种特殊的接口,用于监听特定的事件(如请求创建和销毁、会话创建和销毁、上下文的初始化和销毁)。

当Web应用程序中反生特定事件时,Servlet容器就会自动调用监听器中相应的方法来处理这些事件。

二、监听器的作用?

1、全局初始化和清理

通过实现ServletContextListener接口,开发者可以在Web应用程序启动时执行全局初始化操作(例如加载配置文件、初始化数据库连接池等),并在应用程序关闭时执行清理操作(例如关闭数据库连接池、释放资源等)。

public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {// 应用程序初始化时执行的代码System.out.println("Application is starting...");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {// 应用程序销毁时执行的代码System.out.println("Application is shutting down...");}
}

2、会话管理

通过实现HttpSessionListener接口,开发者可以监控用户会话的创建和销毁。这可以用于统计当前在线用户数量、清理过期会话等。

public class MyHttpSessionListener implements HttpSessionListener {private int activeSessions = 0;@Overridepublic void sessionCreated(HttpSessionEvent se) {activeSessions++;System.out.println("Session created. Total active sessions: " + activeSessions);}@Overridepublic void sessionDestroyed(HttpSessionEvent se) {activeSessions--;System.out.println("Session destroyed. Total active sessions: " + activeSessions);}
}

3、请求和属性监听

通过实现ServletRequestListenerServletRequestAttributeListener接口,开发者可以监听请求的生命周期以及请求属性的变化。这可以用于日志记录、性能监控或动态修改请求属性等。

public class MyServletRequestListener implements ServletRequestListener {@Overridepublic void requestInitialized(ServletRequestEvent sre) {System.out.println("Request initialized...");}@Overridepublic void requestDestroyed(ServletRequestEvent sre) {System.out.println("Request destroyed...");}
}

三、监听器的种类?

1)ServletRequestListener

用途:监听 ServletRequest 的创建和销毁事件。

方法:

  • void requestInitialized(ServletRequestEvent sre):在请求初始化时被调用。

  • void requestDestroyed(ServletRequestEvent sre):在请求销毁时被调用。

代码:

public class MyServletRequestListener implements ServletRequestListener {@Overridepublic void requestInitialized(ServletRequestEvent sre) {System.out.println("Request initialized");}@Overridepublic void requestDestroyed(ServletRequestEvent sre) {System.out.println("Request destroyed");}
}

 

2) ServletRequestAttributeListener

用途监听 ServletRequest 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(ServletRequestAttributeEvent srae):当属性被添加到请求时调用。

  • void attributeRemoved(ServletRequestAttributeEvent srae):当属性从请求中移除时调用。

  • void attributeReplaced(ServletRequestAttributeEvent srae):当请求中的属性被替换时调用。

代码:

public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {@Overridepublic void attributeAdded(ServletRequestAttributeEvent srae) {System.out.println("Attribute added: " + srae.getName());}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent srae) {System.out.println("Attribute removed: " + srae.getName());}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent srae) {System.out.println("Attribute replaced: " + srae.getName());}
}

3)HttpSessionListener

用途:监听 HttpSession 的创建和销毁事件。

方法:

  • void sessionCreated(HttpSessionEvent se):在会话创建时被调用。

  • void sessionDestroyed(HttpSessionEvent se):在会话销毁时被调用。

代码:

public class MyHttpSessionListener implements HttpSessionListener {@Overridepublic void sessionCreated(HttpSessionEvent se) {System.out.println("Session created: " + se.getSession().getId());}@Overridepublic void sessionDestroyed(HttpSessionEvent se) {System.out.println("Session destroyed: " + se.getSession().getId());}
}

4)HttpSessionAttributeListener

用途:监听 HttpSession 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(HttpSessionBindingEvent event):当属性被添加到会话时调用。

  • void attributeRemoved(HttpSessionBindingEvent event):当属性从会话中移除时调用。

  • void attributeReplaced(HttpSessionBindingEvent event):当会话中的属性被替换时调用。

代码:

public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {System.out.println("Session attribute added: " + event.getName());}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {System.out.println("Session attribute removed: " + event.getName());}@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {System.out.println("Session attribute replaced: " + event.getName());}
}

5)ServletContextListener

用途:监听 ServletContext 的初始化和销毁事件。

方法:

  • void contextInitialized(ServletContextEvent sce):在上下文初始化时被调用。

  • void contextDestroyed(ServletContextEvent sce):在上下文销毁时被调用。

代码:

public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("Context initialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("Context destroyed");}
}

6)ServletContextAttributeListener

用途:监听 ServletContext 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(ServletContextAttributeEvent scab):当属性被添加到上下文时调用。

  • void attributeRemoved(ServletContextAttributeEvent scab):当属性从上下文中移除时调用。

  • void attributeReplaced(ServletContextAttributeEvent scab):当上下文中的属性被替换时调用。

代码:

public class MyServletContextAttributeListener implements ServletContextAttributeListener {@Overridepublic void attributeAdded(ServletContextAttributeEvent scab) {System.out.println("Context attribute added: " + scab.getName());}@Overridepublic void attributeRemoved(ServletContextAttributeEvent scab) {System.out.println("Context attribute removed: " + scab.getName());}@Overridepublic void attributeReplaced(ServletContextAttributeEvent scab) {System.out.println("Context attribute replaced: " + scab.getName());}
}

http://www.lryc.cn/news/539091.html

相关文章:

  • Ubuntu下mysql主从复制搭建
  • VirtualBox 中使用 桥接网卡 并设置 MAC 地址
  • Ubuntu 20 掉显卡驱动的解决办法
  • EasyPoi系列之框架集成及基础使用
  • Web后端 Tomcat服务器
  • 【RK3588嵌入式图形编程】-SDL2-构建模块化UI
  • 面向机器学习的Java库与平台简介、适用场景、官方网站、社区网址
  • 基于YOLO11深度学习的心脏超声图像间隔壁检测分割与分析系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标分割、人工智能
  • ubuntu24基于虚拟机无法从主机拖拽文件夹
  • 常用Webpack Loader汇总介绍
  • 剑指 Offer II 023. 两个链表的第一个重合节点
  • 个人搭建CDN加速服务 特网科技
  • 用deepseek学大模型08-卷积神经网络(CNN)
  • 蓝桥杯单片机基础部分——6、555定时器
  • Python学习心得函数
  • 神经网络实验——MLP
  • 配置Api自动生成
  • dify-AI 私有部署可修改前端页面
  • 使用 @Results 注解来手动指定字段映射
  • 数据治理中 大数据处理一般都遵循哪些原则
  • 从0到1:STM32温控系统开发踩坑指南
  • 修改时无条件,可以自定义id条件(通过查询)
  • 工业制造能耗管理新突破,漫途MTIC-ECM平台助力企业绿色转型!
  • 实现一个简单的协同过滤推荐算法
  • eNSP防火墙综合实验
  • 操作系统知识(二)
  • 图论:tarjan 算法求解强连通分量
  • Spring中Bean的四种实例化方法
  • 专利申请要求
  • 解锁 JavaScript 异步编程:Promise 链式操作、async/await 与 Promise.all 深度剖析