|
| 1 | +# Java代码审计-Listener |
| 2 | + |
| 3 | +Listener 表示监听器,可以监听 `application`,`session`,`request` 三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件。 `application` 是 `ServletContext` 类型的对象。`ServletContext` 代表整个web应用,在服务器启动的时候,tomcat会自动创建该对象。在服务器关闭时会自动销毁该对象。 |
| 4 | + |
| 5 | +## 1. Listener分类 |
| 6 | + |
| 7 | +在JavaWeb中提供了8个监听器: |
| 8 | + |
| 9 | +``` |
| 10 | +6个常规监听器 |
| 11 | + ServletContext |
| 12 | + ServletContextListener(上下文监听器,监听ServletContext对象的创建和销毁事件,用于监听Web应用程序的启动和关闭事件。) |
| 13 | + ServletContextAttributeListener(属性监听,监听ServletContext中属性的添加、修改和删除事件。) |
| 14 | + HttpSession |
| 15 | + HttpSessionListener(会话监听器,监听HttpSession对象的创建和销毁事件,用于监听会话的创建和销毁事件。) |
| 16 | + HttpSessionAttributeListener(属性监听,监听HttpSession中属性的添加、修改和删除事件。) |
| 17 | + ServletRequest |
| 18 | + ServletRequestListener(请求监听器,监听ServletRequest对象的创建和销毁事件,用于监听请求的创建和销毁事件。) |
| 19 | + ServletRequestAttributeListener(属性监听,监听ServletRequest中属性的添加、修改和删除事件。) |
| 20 | +
|
| 21 | +2个感知监听 |
| 22 | + HttpSessionBindingListener |
| 23 | + HttpSessionActivationListener |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +## 2. Listener 生命周期 |
| 29 | + |
| 30 | +Listener 的生命周期由容器管理,容器会在适当的时机调用监听器的方法。让我们简要了解一下这些生命周期方法。 |
| 31 | + |
| 32 | +##### `ServletContextListener` 生命周期 |
| 33 | + |
| 34 | + `ServletContextListener` 接口定义了两个方法: |
| 35 | + |
| 36 | +- **`contextInitialized(ServletContextEvent sce)`**:在 Web 应用程序初始化时被调用。 |
| 37 | +- **`contextDestroyed(ServletContextEvent sce)`**:在 Web 应用程序销毁时被调用。 |
| 38 | + |
| 39 | +##### `HttpSessionListener` 生命周期 |
| 40 | + |
| 41 | +`HttpSessionListener` 接口也定义了两个方法: |
| 42 | + |
| 43 | +- **`sessionCreated(HttpSessionEvent se)`**:在会话创建时被调用。 |
| 44 | +- **`sessionDestroyed(HttpSessionEvent se)`**:在会话销毁时被调用。 |
| 45 | + |
| 46 | +##### `ServletRequestListener` 生命周期 |
| 47 | + |
| 48 | +`ServletRequestListener` 接口同样定义了两个方法: |
| 49 | + |
| 50 | +- **`requestInitialized(ServletRequestEvent sre)`**:在请求创建时被调用。 |
| 51 | +- **`requestDestroyed(ServletRequestEvent sre)`**:在请求销毁时被调用。 |
| 52 | + |
| 53 | +## 3. Listener实现步骤 |
| 54 | + |
| 55 | +以`ServletContextListener`为例,`ServletContextListener` 用于监听 Web 应用程序的启动和关闭事件。 |
| 56 | + |
| 57 | +- 定义类,实现`ServletContextListener` 接口 |
| 58 | +- 重写所有抽象方法,我们可以在 `contextInitialized` 方法中执行一些初始化操作,在 `contextDestroyed` 方法中执行清理操作。 |
| 59 | +- 使用 `@WebListener` 进行配置(或者使用`web.xml : <listener></listener>`) |
| 60 | +- 启动tomcat,查看console的输出 |
| 61 | + |
| 62 | +```java |
| 63 | +package com.echo0d; |
| 64 | + |
| 65 | +import javax.servlet.ServletContextEvent; |
| 66 | +import javax.servlet.ServletContextListener; |
| 67 | +import javax.servlet.annotation.WebListener; |
| 68 | + |
| 69 | +@WebListener |
| 70 | +public class MyServletContextListener implements ServletContextListener { |
| 71 | + |
| 72 | + @Override |
| 73 | + public void contextInitialized(ServletContextEvent sce) { |
| 74 | + // 应用程序启动时执行的初始化操作 |
| 75 | + System.out.println("应用程序已启动"); |
| 76 | + // 加载配置文件、初始化数据库连接池等 |
| 77 | + // ... |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void contextDestroyed(ServletContextEvent sce) { |
| 82 | + // 应用程序关闭时执行的清理操作 |
| 83 | + System.out.println("应用程序即将关闭"); |
| 84 | + // 关闭数据库连接池、释放资源等 |
| 85 | + // ... |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments