在tomcat6中,我用表单用get方法提交,但如果其中表单参数有中文,提交后接受到的就为就为乱码,
注:因为我们要取到get方法提交的url,所以不能换成post方法,请问各位前辈,在不用改tomcat配置和仍热用get方法情况下,是否有其他中文乱码的解决办法
注:因为我们要取到get方法提交的url,所以不能换成post方法,请问各位前辈,在不用改tomcat配置和仍热用get方法情况下,是否有其他中文乱码的解决办法
调试欢乐多
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" /> 2.用encodeURI函数处理get的url:
如:encodeURI ('site.action?name=中文乱码')
String name =URLDecoder.decode(request.getParameter("name"),"utf-8");
get请求时
new String(request.getParameter("userAnswer").getBytes("iso-8859-1"),"utf-8")
这样获取可以
var out, i, len, c; out = "";
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}function utf8to16(str) {
var out, i, len, c;
var char2, char3; out = "";
len = str.length;
i = 0;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> 4){
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}我就会这个:P
<a href="......?action=<%=java.net.URLEncoder.encode("中文参数","UTF-8")%>">参数传递</a>后台:
String action = new String(request.getParameter("action").getBytes("UTF-8"));
URLDecode.decode
对链接处理下