Reception to get backstage passes with Chinese data appears garbled Chinese

The same before and after the blog :( station file encoding) Front pass background Chinese garbled

April 4 supplement

When the first sentence is not jsp page below to view the source code a page or publish the first sentence is gone

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

The main idea, the front desk twice encoding, decoding twice the background, must be twice, the first time the character encoding, the second symbol% ​​coding (eg encoded effect: http: // localhost: 8080 / EOMS / manager / edituser? batch = 20190318105448 (% 25E6% 259B% 25B4% 25E6% 25AD% 25A3))

<!--前台:-->
url = test? value = encodeURI(encodeURI(参数值))
<!--后台: -->
String value = request.getParam("value");
value = new String(value.getBytes("iso-8859-1"),"utf-8");
value =  java.net.URLDecoder.decode(value,"UTF-8");

Here is the demo

Reception Code

<!--先使用两次encodeURI()对参数进行编码,再到后台进行解码-->
<a href='/EOMS/manager/edituser?batch="+encodeURI(encodeURI(data.rows[i].batch))+"' class='iconfont icon-bianji'></a>

Background code

/**
 * 进入批次编辑页面
 */
@RequestMapping("edituser")
public String edituser(String batch, Model model) {
    <!--接受前台传递过来的参数并进行解码-->
    try {
        batch = new String(batch.getBytes("iso-8859-1"),"utf-8");
        batch = java.net.URLDecoder.decode(batch,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    
    model.addAttribute("batch", batch);
    return "/views/manager/edituser";

}

Full article:

地址栏中出现汉字的情况有两种,一种是汉字出现在URL的路径部分,一种是汉字出现在URL的传参的部分。

  第二种情况的时候必须采用编码后传参,接受时解码的方式完成传参。

  js中编码有escape(), encodeURI(), 
encodeURIComponent()三个常用的方法。escape()常常用在提交页面和处理页面的编码格式相同的情况下(比如它们都是GB2312),escape方法已经过时,且无法编码url中的中文。所以常用encodeURI()和encodeURIComponent(),它们的用法基本相同,区别在于encodeURIComponent()也对"?"等特殊字符进行编码。

  一开始遇到中文参数的时候,使用encodeURI()进行了一遍编码,传过去后,发现解码出现问题,于是想到可能是编码方法使用错误,于是使用escape()方法,这时发现解码时抛出isHexDigit异常。借助百度搜索isHexDigit异常,发现原来,是escape()方法造成了异常,同时了解了浏览器传递地址的一些原理,在浏览器地址栏里,浏览器认为%是个转义字符,浏览器会把%与%之间的编码,两位两位取出后进行解码,然后再传递给处理页面,然后由处理页面进行再次解码。由此我想到一直使用encodeURI方法是正确的,只是需要使用两次encodeURI方法,例如encodeURI(encodeURI("中文"));第一次是把中文编码成%xy的格式, 
第二次是对%xy中的%进行编码,%编码成%。整个传参过程大体应该是:提交页面使用encodeURI(encodeURI("中文"))编码,把最后的编码结果%xy传递给处理页面的过程中,浏览器获取URL地址(注意 
openModelDialog方法, 浏览器获取不到参数编码)后解码成%xy,然后把%xy传递给处理页面,处理页面使用

  URLDecoder.decode(request.getParameter("参数名"),"UTF-8");完成解码。

  总结:

  1、汉字出现在URL路径部分的时候不需要编码解码;

  2、使用encodeURI进行2次编码;

  3、在openModelDialog()打开的模式窗体里没办法用request.getParameter正确获取参数;

  客户端和服务器在传递数据时可以用过滤器filter解决字符编码问题,但filter只能解决post方式提交的数据。对于get方式,可以使用两次encodeURI(encodeURI(“中文”))并在服务器中使用URLDecoder.decode(“中文”, 
"UTF-8");

  今天用Ajax校验数据时也遇到这个问题,尽管页面、类和web容器都统一了字符编码,提交的数据依然是乱码,所以就采用了2次encodeURI()编码方式,乱码问题就解决了。

  在页面中:

  /exportExcel.topinfo?ls="+encodeURI(encodeURI(_tmplsgx))+"&zt="+encodeURI(encodeURI(_tmpzt))

  在action中

  String ls=request.getParameter("ls");

  ls = new String(ls.getBytes("iso-8859-1"),"utf-8");

  ls = java.net.URLDecoder.decode(ls,"UTF-8");

  这样乱码就解决了。

Guess you like

Origin www.cnblogs.com/yonyong/p/11299005.html