springboot使用servlet原生组件servlet、listener、filter

servlet原生组件servlet、listener、filter

自定义servlet
  • 方式一

首先实现HttpServlet接口,使用@WebServlet注解声明映射

@WebServlet(urlPatterns = "/*")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("MyServlet");
    }
}

使用注解扫描添加Servlet

@SpringBootApplication
@ServletComponentScan(basePackages = "com.szm.servlet")
public class SpringbootApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootApplication.class, args);
    }
}
  • 方式二

使用ServletRegistrationBean添加

@Configuration
public class MyServletConfiguration {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new MyServlet(), "/*");
    }

}
自定义filter
  • 方式一

使用@WebFilter注解,一定要使用@ServletComponentScan扫描到filter,或者加上@Component注解,单独的@WebFilter是不会生效的

@WebFilter(urlPatterns = "/*")
public class TestFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("TestFilter");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
  • 方式二

使用FilterRegistrationBean添加

@Configuration
public class MyServletConfiguration {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new MyServlet(), "/*");
    }

     @Bean
    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new TestFilter(), servletRegistrationBean());
//        return filterRegistrationBean;
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new TestFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
      }

  
}
自定义listener
  • 方式一

使用@WebListener,一定要使用@ServletComponentScan扫描到listener,或者使用@Component声明listener类

@WebListener
public class TestListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("TestListener");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
  • 方式二

使用ServletListenerRegistrationBean添加

@Configuration
public class MyServletConfiguration {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new MyServlet(), "/*");
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new TestFilter(), servletRegistrationBean());
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
       return new ServletListenerRegistrationBean(new MyListener());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值