16. Web native component injection (Servlet, Filter, Listener)


[Shang Silicon Valley] SpringBoot2 Zero-Basic Introductory Tutorial - Lecturer: Lei Fengyang
Notes

The road is still going on, the dream is still waiting

1. Use the native Servlet API

It is recommended to use the native Servlet API in this way

1.1、Servlet

Use the native annotations provided by Servlet 3.0 and above.

@WebServlet(urlPatterns = “/my”)

Effect: No need to write configuration in web.xml, direct response, no Spring interceptor, urlPatterns: access path.

Specify the package to scan native Servlet components in the main configuration class: @ServletComponentScan(basePackages = “com.atguigu.admin.servlet”), if not written, all packages under the group configuration class will be scanned by default.

@WebServlet(urlPatterns = "/my") 
public class MyServlet extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().write("6666");
    }
}

1.2、Filter

Use the native annotations provided by Servlet 3.0 and above.

@WebFilter(urlPatterns={“/css/“,”/images/”})

Effect: No need to write configuration in web.xml, urlPatterns: interception path, multiple paths can be intercepted.

@WebFilter(urlPatterns={
    
    "/my"})
public class MyFilter implements Filter {
    
    

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
        System.out.println("MyFilter初始化完成");
    }

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

    @Override
    public void destroy() {
    
    
        System.out.println("MyFilter销毁");
    }
}

1.3、Listener

Use the native annotations provided by Servlet 3.0 and above.

@WebListener

@WebListener
public class MyServletContextListener implements ServletContextListener {
    
    

    @Override
    public void contextInitialized(ServletContextEvent sce) {
    
    
        System.out.println("监听项目初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    
    
        System.out.println("监听项目销毁");
    }
}

1.4. The reason why the native servlet is not intercepted by the interceptor

There are two Servlets in the system (spring boot: DispatcherServlet–>/ and MyServlet -->/my)

1.5. Extension: How to register DispatchServlet

● The DispatcherServlet property is automatically configured in the container and bound to WebMvcProperties; the corresponding configuration file configuration item is spring.mvc.
● Configure DispatcherServlet through ServletRegistrationBean.
● The default mapping is / path.

insert image description here
Before using Tomcat-Servlet for development, multiple servlet paths overlap each other, and multiple Servlets can handle the same layer path, using the precise optimization principle.

Example:

Two paths
A: /my/
B: /my/1
If /my/1 is sent, the B path will be used, and if /my/2 is sent, the A path will be used.

2. Use RegistrationBean

You can also use ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean to register components.

@Configuration
public class MyRegistConfig {
    
    

    // servlet
    @Bean
    public ServletRegistrationBean myServlet() {
    
    
        MyServlet servlet = new MyServlet();
        return new ServletRegistrationBean<MyServlet>(servlet, "/my");
    }

    // filter
    @Bean
    public FilterRegistrationBean myFilter() {
    
    
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean filter = new FilterRegistrationBean(new MyFilter());
        filter.setUrlPatterns(Arrays.asList("/my"));
        return filter;
    }

    // listener
    @Bean
    public ServletListenerRegistrationBean myListener() {
    
    
        MyServletContextListener listener = new MyServletContextListener();
        return new ServletListenerRegistrationBean(listener);
    }
}

Guess you like

Origin blog.csdn.net/zhao854116434/article/details/130251261