转自:过了很多天来记录,具体忘了地址当初网址了
需要一个自定义listener类继承ServletRequestListener,并在类上添加@WebListener 注解
package com.zjxt.demo.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyRequestListener implements ServletRequestListener {
private Logger logger = LoggerFactory.getLogger(MyRequestListener.class);
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
logger.info("------------------请求销毁");
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
ServletRequest servletRequest = servletRequestEvent.getServletRequest();
String id1 = servletRequest.getParameter("id");
logger.info("------------------请求创建"+id1);
}
}
servletRequest有很多方法,可以获取其中的信息
然后再启动类上添加 @ServletComponentScan 注解扫描该listener的包
package com.zjxt.demo;
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan(basePackages = "com.zjxt.demo.listener")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app =new SpringApplication(Application.class);
app.run(DemoApplication.class, args);
}
}
然后就可以启动启动类,访问该项目,然后就会看到监听的信息。