(1)Tomcat 的 conf 下的server.xml文件中将
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
里添加上后面3个编码的设置
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
useBodyEncodingForURI="true"
IEncoding="utf-8"
URIEncoding="utf-8"/>
(2)
然后在servlet中配置如下:
如果你用的是 post方式
解决方案:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding( "utf-8" );
get方式
String string = request。getParamers("");
String = new String(string.getBytes("ISO8859-1","utf-8"));
(3)jsp页面中 pageEncoding="UTF-8"
(4)如果你用的是Eclipse或者MyEclipse的话,那么你在window 下的preferences中的左上角的输入框中输入spelling 在spelling这个页面中看看Encoding 默认是否设置为utf-8
然而,我是因为
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html:charset=utf-8");
没有放在最前面出错的哈哈哈
(这篇只用于错误记录,不做原理说明)