SpringBoot和web组件

1 拦截器

拦截器是SpringMVC中的一种对象,能拦截对Controller的请求。

自定义拦截器步骤:
(1)创建类实现SpringMVC框架中的HandlerInterciptor接口

(2)在SpringMVC配置文件中声明拦截器

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

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) 创建Servlet类,继承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) 注册Servlet

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

3 Filter

Filter是Servlet规范中的过滤器。可以处理请求,对请求的参数,属性进行调整。常常在过滤器中处理字符编码。
(1) 创建Filter类,继承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) 注册Filter

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

4 字符集过滤器

CharacterEncodingFilter: 解决post请求中乱码的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值