一、在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. Mysql数据操作《一》数据的增删改

    插入数据INSERT 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); 语法二: INSERT INT ...

  2. OCP 12c最新考试原题及答案(071-7)

    7.(5-1) choose two:View the Exhibit and examine the structure of the PRODUCTS table.Which two tasks ...

  3. Xamarin Android Webview中JS调用App中的C#方法

    参考链接:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_fro ...

  4. openstack中安装包与组件

    keystone openstack-keystone:验证服务,openstack 中的所有组件的验证以及用户验证,权限,目录等服务. python-openstackclient:命令行,安装以后 ...

  5. redis 学习笔记(一)

    redis 基本类型 String 基本操作: GET 获取存储在给定键中的值 SET 设置存储在给定键中的值 DEL 删除存储在给定键中的值 List 基本操作: LPUSH/RPUSH 从左/右推 ...

  6. DEM反应添加顺序注意问题

    在含有DEM反应的dat中,均相反应的block要在DEM反应之前,例如: @(RXNS) (some reaction equations) @(END) @(DES_RXNS) (some rea ...

  7. Python——Django学习笔记

    Django——一个封装好的神奇框架 若本文有任何内容错误,望各位大佬指出批评,并请直接联系作者修改,谢谢!小白学习不易. 一.简要模型 模型类操作数据表: python manage.py shel ...

  8. django 创建QueryDict类型报错

    报错信息:django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are ...

  9. 第九次 Scrum Meeting

    第九次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/4/14 19:00 60min 新主楼F座2F 附Github仓库:WEDO 例会照片 工作情况总结(4.14) ...

  10. maven相关的说明以及通过它来创建项目

    1.什么是maven maven的本质是一个项目构建工具 2.maven的作用 那么作为一个项目构建工具我们又为什么要使用它以及好处呢 首先项目构建的本质是什么:项目代码从源代码到程序文件的过程是代码 ...