过滤器说明
- 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();}}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- three.js 如何使用webgl实现3D储能,3D储能站,3D智慧储能、储能柜的三维可视化解决方案——第十七课
- JavaScript常用工具函数
- 乳胶漆含甲醛吗几天才能去掉甲醛 除甲醛常用的几种方法
- 支持JDK19虚拟线程的web框架之四:看源码,了解quarkus如何支持虚拟线程
- 常用Python库整理
- SpringBoot 02: 初识SpringBoot
- String常用API
- 33 《吐血整理》高级系列教程-吃透Fiddler抓包教程-Fiddler如何抓取WebSocket数据包
- 超详细 SpringBoot 整合 Elasticsearch .md
- Websocket集群解决方案