Home > Web Front-end > JS Tutorial > body text

Solution to the problem of garbled characters when passing Chinese parameters from js to action_javascript skills

WBOY
Release: 2016-05-16 17:06:26
Original
1107 people have browsed it

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:

Copy code The code is 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))

Action: Decode when receiving Chinese parameters. Use: java.net.URLDecoder.decode(realName, "UTF-8");

For example:
Copy code The code is 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template