整合Servlet、Filter、Listener(SpringBoot)

一般情况下, 使用Spring, SpringMVC 这些框架之后, 基本上就告别Servlet、Filter、Listener了, 但是有时候整合一些第三方框架时, 可能还是不得不使用Servlet、Filter、Listener。

  • 整合Servlet

使用@WebServlet("/my")注解

@WebServlet("/my")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        System.out.println("MyServlet>>>doGet");
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
        System.out.println("MyServlet>>>doPost");
    }

}
  • 整合Filter
@WebFilter("/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter>>>init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter>>>doFilter");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("MyFilter>>>destroy");
    }
}
  • 整合Litener
  • @WebListener
    public class MyListener implements ServletRequestListener {
        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            System.out.println("MyListener>>>requestDestroyed");
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            System.out.println("MyListener>>>requestInitialized");
        }
    }

    最关键的一步是启动这些配置, 需要告诉程序, 

    @SpringBootApplication
    //添加注解, 进行配置扫描
    @ServletComponentScan
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }

     

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值