SpringBoot and web components

1 interceptor

An interceptor is an object in SpringMVC that can intercept requests to the Controller.

Custom interceptor steps:
(1) Create a class that implements the HandlerInterciptor interface in the SpringMVC framework

(2) Declare the interceptor in the SpringMVC configuration file

<mvc:interciptors> 
	<mvc:interciptor> 
		<mvc:path="url"/>
		<bean class="拦截器全限定名称"/>
	</mvc:interciptor>
</mvc:interciptors> 

Using interceptors in SpringBoot

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 功能描述
 *
 * @since 2022-09-03
 */
public class MyAppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {
        // 创建拦截器对象
        HandlerInterceptor handlerInterceptor = new LoginInterceptor();

        // 指定拦截请求uri地址
        String[] path = new String[] {"/user/**"};
        // 指定不拦截的地址
        String[] execludePath = new String[] {"/user/login"};
        interceptorRegistry.addInterceptor(handlerInterceptor)
            .addPathPatterns(path)
            .excludePathPatterns(execludePath);
    }
}

2 Servlet

(1) Create a Servlet class and inherit HttpServlet

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.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("welcome to use servlet");
        out.flush();
        out.close();
    }
}

(1) Register Servlet

@Configuration
public class WebApplicationConfig {
    
    
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
    
    
        ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet(), "/myservlet");
        return bean;
    }
}

3 Filter

Filter is a filter in the Servlet specification. It can process the request and adjust the parameters and attributes of the request. Character encodings are often handled in filters.
(1) Create a Filter class and inherit HttpServlet

public class MyFilter implements Filter {
    
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
        Filter.super.init(filterConfig);
    }

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

    @Override
    public void destroy() {
    
    
        Filter.super.destroy();
    }
}

(1) Register Filter

    public FilterRegistrationBean filterRegistrationBean () {
    
    
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new MyFilter());
        bean.addUrlPatterns("/user/*");
        return bean;
    }

4 charset filters

CharacterEncodingFilter: Solve the problem of garbled characters in post requests

Guess you like

Origin blog.csdn.net/kaikai_sk/article/details/126663011