欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot 中Servlet,Filter,Listener的代码配置

程序员文章站 2022-06-19 14:47:18
SpringBoot创建web项目中,有可能是以jar的方式创建的.创建之后没有对应的webapp目录,也没有对应的web.xml文件.而web.xml文件通常会配置Servlet,Filter,Listener等信息.SpringBoot为我们提供了ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean等类配置相关信息.1.Servlet配置1)新建servlet类public class MySe...

SpringBoot创建web项目中,有可能是以jar的方式创建的.创建之后没有对应的webapp目录,也没有对应的web.xml文件.而web.xml文件通常会配置Servlet,Filter,Listener等信息.SpringBoot为我们提供了ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean等类配置相关信息.
1.Servlet配置
1)新建servlet类

public class MyServlet extends HttpServlet{


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("hello servlet...");
    }
}

2)通过ServletRegistrationBean注册

   /**
     * 注入servlet   代替web.xml配置
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new MyServlet(), "/myServlet");
    }

3)页面显示:
SpringBoot 中Servlet,Filter,Listener的代码配置

2.Filter配置
1)新建Filter类

package com.oxx.springboot.component;


import javax.servlet.*;
import java.io.IOException;

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........");
    }
}

2)通过FilterRegistrationBean注册:

/**
     * 注入filter   代替web.xml配置
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        return new FilterRegistrationBean(new MyFilter(), servletRegistrationBean());
    }

3)启动项目,退出项目 控制台显示:
SpringBoot 中Servlet,Filter,Listener的代码配置
配置的是自定义的servlet,过滤的是/myServlet,需要访问一次才会出现doFilter…点击红色框框按钮退出才会显示destroy

3.Listener配置
1)新建Listener类:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("-----------------MyServletContextListener  init ......");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("---------------MyServletContextListener  destroy........");
    }
}

2)通过ServletListenerRegistrationBean注册Listener:

 /**
     * 注入listener  代替web.xml配置
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        return new ServletListenerRegistrationBean(new MyServletContextListener());
    }

3)启动项目,退出项目 控制台显示
SpringBoot 中Servlet,Filter,Listener的代码配置
点击红色框框按钮退出,才回显示destroy…

4.容器相关配置
1)通过代码配置容器相关信息

  /**
     * 代码配置容器相关信息
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
            @Override
            public void customize(TomcatServletWebServerFactory factory) {
                factory.setPort(9999);  //端口
                factory.setContextPath("/tomcat");  //访问路径
            }
        };
    }

2)启动项目 控制台显示:
SpringBoot 中Servlet,Filter,Listener的代码配置

本文地址:https://blog.csdn.net/xindanding/article/details/112002993