一、在web.xml中的配置

    <!-- characterEncodingFilter字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好)-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!--是否强制设置request的编码为encoding,默认false,不建议更改-->
<param-name>forceRequestEncoding</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<!--是否强制设置response的编码为encoding,建议设置为true,下面有关于这个参数的解释-->
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!--这里不能留空或者直接写 ' / ' ,否者不起作用-->
<url-pattern>/*</url-pattern>
</filter-mapping>

二、CharacterEncodingFilter过滤器类浅析

打开该类源码,可以看到该类有三个类属性

private String encoding; //要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好)
private boolean forceRequestEncoding = false; //是否强制设置request的编码为encoding
private boolean forceResponseEncoding = false; //是否强制设置response的编码为encoding

主要方法只有一个,也就是下面这个,代码逻辑很简单,入注释所解释

 @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String encoding = getEncoding();
if (encoding != null) { //如果设置了encoding的值,则根据情况设置request和response的编码
//若设置request强制编码或request本身就没有设置编码
//则设置编码为encoding表示的值
if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(encoding);
}
//若设置response强制编码,则设置编码为encoding表示的值
if (isForceResponseEncoding()) { //请注意这行代码,下面有额外提醒
response.setCharacterEncoding(encoding);
}
}
filterChain.doFilter(request, response);
}
# 额外提醒
if (isForceResponseEncoding()) {
response.setCharacterEncoding(encoding);
}

是在

filterChain.doFilter(request, response);
之前执行的,这也就是说这段代码的作用是设置response的默认编码方式,在之后的代码里是可以根据需求设置为其他编码的,即这里设置的编码可能不是最终的编码

对于get请求中文参数出现乱码解决方法有两个:

修改tomcat配置文件添加编码与工程编码一致,如下:

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

另外一种方法对参数进行重新编码:

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

三、可以自定义拦截器(在web.xml中配置)
 <filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>com.itwang.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class SetCharacterEncodingFilter implements Filter {

    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
} String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){
encoding = "UTF-8";
} //POST:
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
} public void destroy() { } }

参考:https://blog.csdn.net/lianjunzongsiling/article/details/77926370

spring设置字符编码过滤器的更多相关文章

  1. Spring的字符编码过滤器CharacterEncodingFilter

    Spring中的字符编码过滤器,用来解决我们项目中遇到的编码问题. 使用方式特别友好,在web.xml加入: <filter> <description>字符集过滤器</ ...

  2. 深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器

    引用 前几天在论坛上看到一篇帖子,是关于Struts2.0中文乱码的,楼主采用的是spring的字符编码过滤器(CharacterEncodingFilter)统一编码为GBK,前台提交表单数据到Ac ...

  3. Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生

    同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...

  4. SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题

    1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...

  5. 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题

    当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...

  6. Jsp字符编码过滤器

    通过此过滤器,可以实现统一将编码设置为UTF-8. 1.首先在web.xml中配置,添加如下代码: <!-- 过滤器 --> <filter> <filter-name& ...

  7. web.xml 设置字符编码

    个人理解  就是为了防止在前端输入的数据到了后台发生乱码 直接复制到web.xml里面就可以使用 亲测 能用!!! <!-- 前端过滤器设置字符编码 --> <filter> ...

  8. Servlet字符编码过滤器

    在Java Web程序开发中,由于Web容器内部使用编码格式并不支持中文字符集,所以,处理浏览器请求中的中文数据就会出现乱码的现象.由于Web容器使用了ISO-8859-1的编码格式,所以在Web应用 ...

  9. springmvc字符编码过滤器CharacterEncodingFilter浅析

      一.在web.xml中的配置 <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>cha ...

随机推荐

  1. Eclipse无法正常创建android工程解决方法!

    我最近升级了安卓的SDK,升级后发现无法在Eclipse中创建android工程了,创建的工程完毕后,没有自动生成这个工程的activity和对应的布局文件,上网搜索一番,最后终于解决了.可能是升级后 ...

  2. Windows下使用DOS命令进入MySQL数据库

    先要配置环境变量 MYSQL_HOME : D:\mysql-8.0.11-winx64 Path:%MYSQL_HOME%\bin 1)新建MYSQL_HOME变量,并配置:C:\Program F ...

  3. Python——深拷贝和浅拷贝

    深拷贝.浅拷贝 1. 浅拷贝 浅拷贝是对于一个对象的顶层拷贝 import copy a = [[1, 2], 3] b = copy.copy(a) print(id(a)) print(id(b) ...

  4. myeclipse上down出的svn项目,文件后面不显示版本号和修改人

    找到 windows ->preferences->General->Appearance->Lable Decorations 下拉找到svn 勾选上

  5. 条目二十八《正确理解由reverse_iterator的base()成员函数所产生的iterator的用法》

    条目二十八<正确理解由reverse_iterator的base()成员函数所产生的iterator的用法> 迭代器的种类一共有四种,上面已经说过了.这里就不再次写出来. 这一个条目主要是 ...

  6. linux I/O函数使用

    一.lseek lseek函数的作用是用来重新定位文件读写的位移. 头文件以及函数声明 #include <sys/types.h> #include <unistd.h> o ...

  7. PostMan --API调试工具

    https://blog.csdn.net/fxbin123/article/details/80428216

  8. rest-assured之验证响应数据(Verifying Response Data)

    前面的文章中已经介绍过了如果获得响应数据,接下来我们来介绍一下应该如何来验证这些获得的响应数据,比如验证状态码.状态行.cookies.header.content-type以及body体. 1.验证 ...

  9. Python中的正斜杠/与反斜杠\

    知识点: 1. "/"左倾斜是正斜杠,"\"右倾斜是反斜杠,可以记为:除号是正斜杠 2. 对于目录分隔符,Unix和Web用正斜杠/,Windows用反斜杠\. ...

  10. 浅析PHP反序列化漏洞之PHP常见魔术方法(一)

    作为一个学习web安全的菜鸟,前段时间被人问到PHP反序列化相关的问题,以前的博客中是有这样一篇反序列化漏洞的利用文章的.但是好久过去了,好多的东西已经记得不是很清楚.所以这里尽可能写一篇详细点的文章 ...