<s id="2e0qm"><div id="2e0qm"></div></s>
<div id="2e0qm"><button id="2e0qm"></button></div>
<small id="2e0qm"></small>
<div id="2e0qm"></div>
<div id="2e0qm"><s id="2e0qm"></s></div>
<div id="2e0qm"><button id="2e0qm"></button></div>
主頁 > 知識庫 > 網絡編程 > JSP/Java >

Java中正確解碼用Javascript:escape()編碼的中文字符

來源:中國IT實驗室 作者:佚名 發表于:2013-07-15 15:21  點擊:
做頁面開發時使用了Javascript 的escape()函數進行URL編碼,如果url中有中文,結果在JSP或Servlet中用request.getParameter()時取不到參 數,只返回一個空值。經過Baidu的搜索,結合我自己的工作實際,再加上網友的一些幫助,終于找到了問題所在及解決的辦法
做頁面開發時使用了Javascript 的escape()函數進行URL編碼,如果url中有中文,結果在JSP或Servlet中用request.getParameter()時取不到參 數,只返回一個空值。經過Baidu的搜索,結合我自己的工作實際,再加上網友的一些幫助,終于找到了問題所在及解決的辦法。  本來Javascript 中的escape()是將中文按ISO-8859-1字符集進行URL編碼的,那樣通過 request.getParameter()是能直接獲取到請求參數的,但后來的Javascript將escape()換成了Unicode字符集編 碼,如此一來,在JSP和Servlet中就沒法直接拿到請求參數了,而且此時的request獲得的參數值都是空值,想了好久,也測試了不少,但是還是百思不得其解。 最后借助于網絡,終于解決了此問題,我露出了燦爛的笑容?
  解決辦法:
  1、首先對中文字符進行兩次escape()編碼,如要傳參數name,值為“黃修群”,則url的格式 為....name=escape(escape("黃修群")),這樣一來,在request.getParameter()就能取到編碼后的參數了, 但是參數去編碼的形式:如下:
  %u9EC4%u4FEE%u7FA4,
  獲得的頁面參數為:獲取request參數值-->%u9EC4%u4FEE%u7FA4
  此時在使用java.net.URIDecoder.decode()方法,則出現解碼錯誤,錯誤信息如下:
  2010-5-19 15:28:03 org.apache.catalina.core.ApplicationDispatcher invoke
  嚴重: Servlet.service() for servlet jsp threw exception
  java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "u9"
  at java.net.URLDecoder.decode(URLDecoder.java:173)
  at com.mapgis.vfd.plugins.vfdwebserver.VFDWebServer.getPageParameterList(VFDWebServer.java:674)
  可見是解碼出現了問題了
  也許有人問,那我在客戶端非要只用一次escape方法編碼,會出現什么樣的結果?請看下面:
  2010-5-19 14:59:38 org.apache.tomcat.util.http.Parameters processParameters
  警告: Parameters: Character decoding failed. Parameter skipped.
  java.io.CharConversionException: isHexDigit
  at org.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:88)
  at org.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:49)
  at org.apache.tomcat.util.http.Parameters.urlDecode(Parameters.java:412)
  at org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:394)
  。。。。。
  看到上面的提示信息,可以斷定是解碼出現問題了,而獲得的請求對象去是空的,如下:
  獲取request參數值-->null
  2、由于取到的參數是 u9EC4%u4FEE%u7FA4格式的,沒法用常規的URLDecoder.decode()來進行解碼,還好,這世上的牛人夠多,在網上直接找到了 一個工具類,能實現 Javascript中escape()及unescape()式的編解碼,源代碼如下:
  [java]
  /**
  * JavaScript escape/unescape 編碼的 Java 實現
  * author jackyz
  * keep this copyright info while using this method by free
  */
  public class Escape {
  private final static String[] hex = {
  "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
  "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
  "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
  "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
  "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
  "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
  "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
  "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F",
  "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F",
  "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F",
  "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF",
  "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF",
  "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF",
  "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF",
  "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF",
  "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"
  };
  private final static byte[] val = {
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F
  };

有幫助
(0)
0%
沒幫助
(0)
0%
真人女荫道口100种图片
<s id="2e0qm"><div id="2e0qm"></div></s>
<div id="2e0qm"><button id="2e0qm"></button></div>
<small id="2e0qm"></small>
<div id="2e0qm"></div>
<div id="2e0qm"><s id="2e0qm"></s></div>
<div id="2e0qm"><button id="2e0qm"></button></div>