js中有两个函数(escape,unescape)~!ajax提交表单:为了出现中文时不出现乱码需要经过escape进行转码通过用escape函数将中文转成如:“%u4E2D%u534E%u4EBA%u6C11%u5171%u548C%u56FD”的编码当要显示到页面时就要通过unescape函数进行解码 方可显示正常。问题是现在通过escape转码后 转码后的数据将会存入到后台 ,后台中却不知如何进行解码 ?请问高手们 在 java后台如何对用过js escape编码后的数据进行解码?谢谢

解决方案 »

  1.   

    escape现在是已经过时了。。  在前台传中文去后台的时候,要么用post方法,如果用get方法,则需要如下操作才不会乱码:var value =encodeURI(encodeURI(参数值));把value传过去在java后台,用java.net.Decode.decode(request.getParameter("value"),"UTF-8")进行解码
      

  2.   

    使用URLDecoder.decode(str,"utf-8")进行解码; 
      

  3.   


    public String escape(String src) {
      int i;
      char j;
      StringBuffer tmp = new StringBuffer();
      tmp.ensureCapacity(src.length() * 6);
      for (i = 0; i < src.length(); i++) {
       j = src.charAt(i);
       if (Character.isDigit(j) || Character.isLowerCase(j)
         || Character.isUpperCase(j))
        tmp.append(j);
       else if (j < 256) {
        tmp.append("%");
        if (j < 16)
         tmp.append("0");
        tmp.append(Integer.toString(j, 16));
       } else {
        tmp.append("%u");
        tmp.append(Integer.toString(j, 16));
       }
      }
      return tmp.toString();
     }
      
     public String unescape(String src) {
      StringBuffer tmp = new StringBuffer();
      tmp.ensureCapacity(src.length());
      int lastPos = 0, pos = 0;
      char ch;
      while (lastPos < src.length()) {
       pos = src.indexOf("%", lastPos);
       if (pos == lastPos) {
        if (src.charAt(pos + 1) == 'u') {
         ch = (char) Integer.parseInt(src
           .substring(pos + 2, pos + 6), 16);
         tmp.append(ch);
         lastPos = pos + 6;
        } else {
         ch = (char) Integer.parseInt(src
           .substring(pos + 1, pos + 3), 16);
         tmp.append(ch);
         lastPos = pos + 3;
        }
       } else {
        if (pos == -1) {
         tmp.append(src.substring(lastPos));
         lastPos = src.length();
        } else {
         tmp.append(src.substring(lastPos, pos));
         lastPos = pos;
        }
       }
      }
      return tmp.toString();
     }
      

  4.   

    上面说错是:
    java.net.URLDecoder.decode()
      

  5.   

    http://blog.csdn.net/JavaAlpha/archive/2011/04/12/6318917.aspx 参考,
      

  6.   

    encodeURI 或者base64 都可以~~
      

  7.   

    js文字进行编码相关函数诠释~!ajax提交表单:为了出现中文时不出现乱码需要经过escape进行转码通过用escape函数将中文转成如:“%u4E2D%u534E%u4EBA%u6C11%u5171%u548C%u56FD”的编码当要显示到页面时就要通过unescape函数进行解码 方可显示正常。js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1)、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                            

    例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>

    2)、   进行url跳转时可以整体使用encodeURI

    例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度& ct=21");

    3)、   js使用数据时可以使用escape

    [Huoho.Com编辑]

    例如:搜藏中history纪录。

    4)、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下 escape,encodeURI,encodeURIComponent编码结果相同。

    最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-ZencodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a- z,A-ZencodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
    javaScript中URL编码转换,escape() encodeURI() encodeURIComponent 另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在 javascript1.0版本就有。escape()不编码的字符:@*/+相对于使用escape方法,使用encodeURI方法会显得更专业一些。当你需要编码一整个URI的时候,你可以使用此方法,因为URI中的合法字符都不会被编码转换。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
    encodeURI() 不编码的字符: ~!@#$&*()=:/,;?+''encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
    encodeURIComponent()不编码的字符: ~!*()''JAVA后台相应处理:如果是用escape编码 则:通过自定义方法:
    public String unescape(String src) {
      StringBuffer tmp = new StringBuffer();
      tmp.ensureCapacity(src.length());
      int lastPos = 0, pos = 0;
      char ch;
      while (lastPos < src.length()) {
       pos = src.indexOf("%", lastPos);
       if (pos == lastPos) {
        if (src.charAt(pos + 1) == 'u') {
         ch = (char) Integer.parseInt(src
           .substring(pos + 2, pos + 6), 16);
         tmp.append(ch);
         lastPos = pos + 6;
        } else {
         ch = (char) Integer.parseInt(src
           .substring(pos + 1, pos + 3), 16);
         tmp.append(ch);
         lastPos = pos + 3;
        }
       } else {
        if (pos == -1) {
         tmp.append(src.substring(lastPos));
         lastPos = src.length();
        } else {
         tmp.append(src.substring(lastPos, pos));
         lastPos = pos;
        }
       }
      }
      return tmp.toString();
     }如果是用encodeURI编码 则:通过java.net.Decode.decode(request.getParameter("value"),"UTF-8")进行解码 
      

  8.   

    http://blog.csdn.net/czp0608/archive/2011/04/19/6332962.aspx