SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器( 二 )

过滤器说明

  • Filter是Servlet规范中的过滤器,可以处理请求,对请求的参数,属性进行调整 。常常在过滤器中处理字符编码
使用自定义过滤器
  • 创建自定义过滤器类
package com.example.web.filter;import javax.servlet.*;import java.io.IOException;public class MyFilter implements Filter {@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {servletResponse.setContentType("text/html;charset=utf-8");System.out.println("MyFilter的doFilter方法被调用");filterChain.doFilter(servletRequest, servletResponse);}}
  • 注册Filter过滤器对象
package com.example.web.config;import com.example.web.filter.MyFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyFilterConfig {//定义方法,注册过滤器@Beanpublic FilterRegistrationBean filterRegistrationBean(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(new MyFilter());filterRegistrationBean.addUrlPatterns("/user/*");return filterRegistrationBean;}}使用自定义字符集过滤器
  • 创建servlet
package com.example.web.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置响应类型resp.setContentType("text/html");//获取输出流对象PrintWriter out = resp.getWriter();out.println("使用springboot字符过滤器");out.flush();out.close();}}
  • 注册servlet和配置字符集过滤器
package com.example.web.config;import com.example.web.servlet.MyServlet;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.filter.CharacterEncodingFilter;@Configurationpublic class MyWebConfig {//注册servlet@Beanpublic ServletRegistrationBean servletRegistrationBean(){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();servletRegistrationBean.setServlet(new MyServlet());servletRegistrationBean.addUrlMappings("/test");return servletRegistrationBean;}//注册内置的字符过滤器@Beanpublic FilterRegistrationBean filterRegistrationBean(){//内置的字符过滤器CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();//按需求定制字符过滤器characterEncodingFilter.setEncoding("utf-8");characterEncodingFilter.setForceEncoding(true);//注册按照个性化需求配置后的字符过滤器FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(characterEncodingFilter);filterRegistrationBean.addUrlPatterns("/*");return filterRegistrationBean;}}
  • 修改application.properties文件,让自定义的过滤器起作用
#关闭springboot框架默认的字符编码格式:ISO-8859-1server.servlet.encoding.enabled=false使用框架内置的字符集过滤器
  • 创建servlet
package com.example.web.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置响应类型resp.setContentType("text/html");//获取输出流对象PrintWriter out = resp.getWriter();out.println("**** 使用springboot字符过滤器 ****");out.flush();out.close();}}

经验总结扩展阅读