springBoot配置servlet,filter,listener

过滤器,监听器,转发器

1、customFilter过滤器

public class CustomFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init filter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("do filter");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("destroy filter");
    }

}

2、CustomListener监听器

public class CustomListener implements ServletContextListener {

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

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

3、转发器CustomServlet

public class CustomServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet get method");
        doPost(req,resp);
    }

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

方法一:通过注册ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean获得控制

@SpringBootApplication
public class SpringBootDemo71Application {
    @Bean//注册Servlet
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new CustomServlet(), "/roncoo");
    }
    @Bean//注册Filter
    public FilterRegistrationBean filterRegistrationBean() {
        return new FilterRegistrationBean(new CustomFilter());
    }
    @Bean//注册Listener
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        return new ServletListenerRegistrationBean(new CustomListener());
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo71Application.class, args);
    }
}

方法二:通过接口ServletContextInitializer直接注册

@SpringBootApplication
public class SpringBootDemo71Application implements ServletContextInitializer{

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo71Application.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.addServlet("customServlet",new CustomServlet()).addMapping("/zzrdemo");
        servletContext.addFilter("customFilter",new CustomFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST),true,"customServlet");
        servletContext.addListener(new CustomListener());
    }
}

方法三:在SpringBootApplication使用注解@ServletComponentScan,然后在servlet,filter,listener上增加注解即可:

  • servlet上加注解:@WebServlet(value = "/zzrdemo",name = "customServlet")
  • filter上加注解:
    @WebFilter(urlPatterns = "/*",filterName = "customFilter")
  • listener上加注解:@WebListener

你可能感兴趣的:(springboot系列)