目录
Listener
八个Web监听器
注册Listener
父pom文件
pom文件
使用注解方式
ServletListener
HelloServlet
启动类
通过RegistrationBean注册
ServletListener
HelloServlet
MyWebConfig
启动类
运行结果
Listener
作用:监听某个事件的发生,状态的改变
内部机制:接口回调
八个Web监听器
接口名 | 作用 |
---|---|
ServletContextListener | ServletContext对象的创建 |
HttpSessionListener | Session对象的创建 |
ServletRequestListener | Request对象的创建 |
ServletContextAttributeListener | ServletContext属性变化 |
HttpSessionAttributeListener | Session属性变化 |
ServletRequestAttributeListener | Request属性变化 |
HttpSessionActivationListener | Session钝化与活化 |
HttpSessionBindingListener | Session与对象的绑定 |
注册Listener
父pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- <version>3.1.2</version>-->
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chensir</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>8</java.version>
</properties>
<packaging>pom</packaging>
<modules>
<module>servlet</module>
</modules>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- <version>1.7.25</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chensir</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>servlet</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
使用注解方式
使用注解 在启动类上必须加 @ServletComponentScan注解,若使用bean方式注册,则不需要
ServletListener
package com.chensir.javaWeb;
import cn.hutool.core.date.StopWatch;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener
@Slf4j
public class ServletListener implements ServletRequestListener {
// StopWatch存储一组任务耗时时间
StopWatch stopWatch = null;
@Override
public void requestInitialized(ServletRequestEvent sre) {
stopWatch = new StopWatch("监控");
stopWatch.start();
log.info("-------有人来了----------");
}
@Override
public void requestDestroyed(ServletRequestEvent sre){
stopWatch.stop();
log.info("-------有人出来了,耗时:{}----------",stopWatch.getTotalTimeSeconds());
// todo:如果超过多少时间,发信息给开发者
}
}
HelloServlet
package com.chensir.javaWeb;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = {"/hello"})
@Slf4j
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("你好,宝宝");
log.info("给航天员打招呼!");
// 睡眠5s
ThreadUtil.safeSleep(5000);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
}
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
通过RegistrationBean注册
ServletListener
HelloServlet
MyWebConfig
import com.chensir.javaWeb.HelloServlet;
import com.chensir.javaWeb.ServletListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new HelloServlet());
servletRegistrationBean.addUrlMappings("/helloBaby");
return servletRegistrationBean;
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean() {
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new ServletListener());
// 启用
bean.setEnabled(true);
return bean;
}
}
启动类
运行结果