中文乱码问题的解决

  对于新手来说,在项目中难免会遇到中文乱码问题,主要分为POST乱码和GET乱码。

  对于POST乱码可以在web.xml中添加post乱码的filter:

 1 <filter>
 2    <filter-name>CharacterEncodingFilter</filter-name>     
 3    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
 4    <init-param>
 5       <param-name>encoding</param-name>
 6       <param-value>utf-8</param-value>
 7    </init-param>
 8 </filter>
 9 <filter-mapping>
10    <filter-name>CharacterEncodingFilter</filter-name> 
11    <url-pattern>/*</url-pattern>
12 </filter-mapping>

  

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

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

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

  

  2.对参数进行重新编码:

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

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

posted on 2015-10-14 20:22  awhu  阅读(276)  评论(0编辑  收藏  举报

导航