【Spring Boot】031-整合 Servlet 、Filter 和 Listener
一、概述
一般情况下,使用 Spring 、Spring MVC 这些框架之后,基本上就告别了 Servlet 、 Filter 以及 Listenter 了,但是有时在整合一些第三方框架时需要使用到这些。Spring Boot 对于整合这些基本的 Web 组件也提供了很好的支持。
二、代码演示
1、Servlet
package com.zibo.api.web;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
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(urlPatterns = "/hi")
@Slf4j
public class MyServlet extends HttpServlet {
@Override
public void init() throws ServletException {
log.warn("MyServlet => 初始化成功");
super.init();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.warn("MyServlet => doGet");
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.warn("MyServlet => doPost");
super.doPost(req, resp);
}
}
2、Filter
package com.zibo.api.web;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
@Slf4j
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.warn("MyFilter ==> init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.warn("MyFilter ==> doFilter");
}
@Override
public void destroy() {
log.warn("MyFilter ==> destroy");
}
}
3、Listener
package com.zibo.api.web;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener
@Slf4j
public class MyListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
log.warn("MyListener ==> requestDestroyed");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
log.warn("MyListener ==> requestInitialized");
}
}
4、启动类
package com.zibo.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan // 加上此注解以使得上面三个被扫描到
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
三、运行结果
在浏览器随便访问一个接口:http://localhost:8001/hi(此接口需要自己写)
四、遗留问题
MyServlet 中 init() 执行了,但是 doGet() 和 doPost() 未执行!找了很多方案,还是没解决!
五、补充
1、拦截器和过滤器
参考文章
Spring Boot实战:拦截器与过滤器
https://www.cnblogs.com/paddix/p/8365558.html
spring boot 过滤器、拦截器的区别与使用
https://www.cnblogs.com/JonaLin/p/12711146.html
实战Spring Boot 2.0之过滤器和拦截器
https://zhuanlan.zhihu.com/p/45207635
Springboot 过滤器和拦截器详解及使用场景
https://www.cnblogs.com/AIPAOJIAO/p/14017338.html