我的servlet中用了request.setCharacterEncoding("GBK");来解决中文问题,对在地址栏中直接录入中文参数都能反馈正确,但对重定向则发生了中文乱码问题。解决办法:对重定向的URL进行encoding,代码见附文
[@more@]String newUrl = "";
int andIdx = 0;
int equalIdx = whereToGo.indexOf("=");
while (equalIdx > -1) {
newUrl += whereToGo.substring(andIdx, equalIdx + 1);
andIdx = whereToGo.indexOf("&", equalIdx);
if (andIdx != -1) {
newUrl += java.net.URLEncoder.encode(whereToGo.substring(
equalIdx + 1, andIdx), "GBK");
equalIdx = whereToGo.indexOf("=", andIdx);
} else {
newUrl += java.net.URLEncoder.encode(whereToGo
.substring(equalIdx + 1), "GBK");
equalIdx = -1;
}
}
if (newUrl.length() == 0)
newUrl = whereToGo;
response.sendRedirect(newUrl);
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/67736/viewspace-806363/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/67736/viewspace-806363/