When working on a project, I found that Action obtains Chinese parameters in the jsp form. As long as the entire project uses UTF-8 encoding format, there will be no garbled code problem; however, JS is used in JSP and Chinese parameters are passed from JS to Action. There is confusion in Chinese. After asking Baidu several times, there are many opinions.
After practice, we found that the following method can solve the problem of Chinese garbled characters:
In the JS of JSP: Chinese parameters use encodeURI (encodeURI (Chinese parameter)), which is transcoded twice. For example:
function show(next,id,realName){
document.forms['f2'].action="usersearchNextPage?next=" next "&id=" id "&realName=" encodeURI(encodeURI(realName));
document.forms['f2']. submit();
}
where realName is a Chinese parameter. Therefore, realName is transcoded twice in the submitted URL. encodeURI(encodeURI(realName))
Action: Decode when receiving Chinese parameters. Use: java.net.URLDecoder.decode(realName, "UTF-8");
For example:
String realName = ServletActionContext.getRequest().getParameter("realName");
try {
realName = java.net.URLDecoder.decode(realName ,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
After the above processing, the problem is solved.