条件:以 UTF-8编码格式为例
1.检查Eclipse环境,是否为UTF-8。设置方法:Window -> Preferences -> General -> Workspace,设置text file encoding为UTF-8 。
注意:单纯设置项目的环境为UTF-8效果并不好。
2. JSP文件头加<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
3.HTML页面设置<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4.URL参数传中文乱码问题,URL的编码格式采用的是ASCII码,而不是Unicode。解决方式,给传递的参数进行编码转换,在JS中使用两次encodeURI(str)把字符串作为 URI 进行编码。注意一定是两次,然后在取值页面,使用decodeURI(str)进行解码就可以了。
如:"${root}/search.html?words="+ encodeURI(encodeURI($("#mooc-search").val());
decodeURI(words);
5.其他方式:修改服务器编码格式,不太建议的方式。如Tomcat,修改server.xml,给端口加上URIEncoding="UTF-8"
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>