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, I found that the following method can solve the problem of Chinese garbled characters:
In JSP's JS: Chinese parameters use encodeURI (encodeURI(Chinese parameter)), which is transcoded twice. For example:
Copy the code code as follows:
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))
In Action: Decode when receiving Chinese parameters. Use: java.net.URLDecoder.decode(realName, "UTF-8");
like:
Copy the code code as follows:
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.