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

JAVA监听器(学习自用)

一、什么是监听器

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

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

二、监听器的作用?

1、全局初始化和清理

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

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

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // 应用程序销毁时执行的代码
        System.out.println("Application is shutting down...");
    }
}

2、会话管理

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

public class MyHttpSessionListener implements HttpSessionListener {
    private int activeSessions = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        activeSessions++;
        System.out.println("Session created. Total active sessions: " + activeSessions);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        activeSessions--;
        System.out.println("Session destroyed. Total active sessions: " + activeSessions);
    }
}

3、请求和属性监听

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

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

    @Override
    public 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 {
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("Request initialized");
    }

    @Override
    public 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 {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("Attribute added: " + srae.getName());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("Attribute removed: " + srae.getName());
    }

    @Override
    public 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 {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("Session created: " + se.getSession().getId());
    }

    @Override
    public 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 {
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("Session attribute added: " + event.getName());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("Session attribute removed: " + event.getName());
    }

    @Override
    public 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 {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Context initialized");
    }

    @Override
    public 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 {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scab) {
        System.out.println("Context attribute added: " + scab.getName());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        System.out.println("Context attribute removed: " + scab.getName());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        System.out.println("Context attribute replaced: " + scab.getName());
    }
}

相关文章:

  • BEV:车轮接地点车辆修正3D框位置精度
  • 【数据结构】(10) 排序算法
  • 设计模式之适配模式是什么?以及在Spring AOP中的拦截器链的使用源码解析。
  • FreeSwitch的应用类模块
  • Python跨年烟花
  • Redis篇(环境搭建)
  • 【故障处理】- 11g迁19C数据泵报错: ORA-39083 ORA-06598 导致数据库大量对象导入不进去
  • Linux基本指令
  • 微信小程序text组件decode属性的小问题
  • WPF8-常用控件
  • 2025年新型智慧城市整体解决方案下载:顶层规划设计,应用总体建设方案
  • 【Linux Redis】关于用docker拉取Redis后,让虚拟机运行起来redis,并使得其可以连接到虚拟机外的navicat。
  • jessionid
  • linux云服务器部署deepseek,并通过网页访问
  • dify-AI 私有部署可修改前端页面
  • EasyExcel 自定义头信息导出
  • OpenVela——专为AIoT领域打造的开源操作系统
  • 分布式同步锁:原理、实现与应用
  • 交换路由——控制VLAN之间通信
  • axios post请求 接收sse[eventsource]数据的
  • 七方面118项任务,2025年知识产权强国建设推进计划印发
  • 泽连斯基称与特朗普通话讨论停火事宜
  • 2024年上市公司合计实现营业收入71.98万亿元
  • 正荣地产:前4个月销售14.96亿元,控股股东已获委任联合清盘人
  • 以军总参谋长:已进入“决定性打击计划的第二阶段”
  • 司法部:持续规范行政执法行为,加快制定行政执法监督条例