请求乱码
get乱码:
在web.xml配置过滤器
响应乱码
get乱码:
1、直接编码,new String(数组,"utf-8");
String name = request.getParameter("name");
name = new String(name .getBytes("iso8859-1"),"utf-8");
前提是你页面编码就是utf-8,如果是gbk,那上面那句代码后面就改成gbk。
2、修改tomcat的server.xml:找到8080端口配置地方,加上URIEncoding="UTF-8" useBodyEncodingForURI="true"
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" useBodyEncodingForURI="true"
/>
在web.xml配置过滤器
<!-- 配置过滤器,解决post的乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码参是UTF8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
响应乱码
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
}
mysql查看设置编码为utf-8
show variables like 'character%';