post方式乱码很好解决,这边就不介绍了,主要介绍一下get方式乱码解决方法。
1.在后台先使用iso8859-1编码,然后使用utf-8解码
对方使用ISO8859-1编码,你使用utf-8编码,那么你发送前将你的UTF-8转为ISO8859-1 str = new String(str.getBytes("UTF-8"), "ISO-8859-1")或者将utf-8编码的str值发给另一方接收的时候使用String words = new String(str.getBytes("iso-8859-1"),"utf-8")。
2.在js中使用encodeURI,在URLDecoder.decode 解码
通过encodeURI(encodeURI(Text))提交,java代码中用URLDecoder.decode解码:
<script>
$("#loginButton").click(function(){
var Str= "皇总监";
var url = "${path}/item/listItem.do?Str="+Str;
url = encodeURI(encodeURI(iPath));
window.location.href=url;
}
</script>
java代码中取中文:
String documentPath = (String) request.getParameter('Str');
Str = URLDecoder.decode(Str,"utf-8");
3.在服务器server.xml中配置,加上URIEncoding="UTF-8",如下
<Connector connectionTimeout="20000" port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" redirectPort="8443"/>