Tomcat的默认编码是iso-8859-1,需要将请求转化为utf-8
POST乱码:
需要导入的jar包:
spring-web-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
commons-logging-1.2.jar
在web.xml中配置乱码过滤器
<!-- 乱码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GET乱码
1、将接受过来的请求参数转码(iso-8859-1是tomcat的默认编码,需要将tomcat编码后的内容按utf-8编码)
String username;//假设是接受过来的请求参数
username = new String(username.getByte(“iso-8859-1”),”utf-8”);
2、
在
tomcat
的
service.xml
文件中配置
<Connector port="8080"
URIEncoding="utf-8"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />