A solution to the problem of sending garbled Chinese parameters from js to Action

  • 2020-03-30 01:09:22
  • OfStack

When working on the project, I found that the Action took the Chinese parameters in the JSP form, so long as the whole project was in utf-8 encoding format, there would be no garble problem; However, JSP used in JS, and from JS to Action Chinese parameters, will appear Chinese chaos. After asking baidu, the above said a lot.

Through practice, the following methods can be found to solve the problem of Chinese garbled code:

JSP JS: Chinese parameters with encodeURI(encodeURI(Chinese parameters)), after two transcoding. Such as:
 
function show(next,id,realName){ 
document.forms['f2'].action="usersearchNextPage?next="+next+"&id="+id+"&realName="+encodeURI(encodeURI(realName)); 
document.forms['f2'].submit(); 
} 

Where the realName is a Chinese parameter. Therefore, transcode the realName twice in the submitted URL. EncodeURI (encodeURI (realName))

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

Such as:
 
String realName = ServletActionContext.getRequest().getParameter("realName"); 
try { 
realName = java.net.URLDecoder.decode(realName,"UTF-8"); 
} catch (UnsupportedEncodingException e1) { 
e1.printStackTrace(); 
} 

After the above treatment, the problem is solved.

Related articles: