请求乱码
//get请求
在服务器的server.xml 文件中在8080端口处添加URIEncoding="UTF-8"
//post请求
1、在第一次获取请求参数之前设置
requset.setCharacterEncoding("UTF-8)
2、或者自己写一个filte(springMVC已经有写好的)
响应乱码
response.setContentType("text/html;charset=utf-8");
springMVC具体设置
1、在web.xml中配置字符编码过滤器(设置post请求和响应的编码)
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、在服务器的server.xml 修改(解决get请求乱码)
改tomcat中server.xml中Connector的port=“8080”,加上一个 URIEncoding=”utf-8”
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
3、jsp页面编码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
网友评论