程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 如何解決JSP參數傳遞亂碼

如何解決JSP參數傳遞亂碼

編輯:關於JSP

      計算機生於美國,英語是他的母語,而英語以外的其它語言對他來說都是外語。他跟我們一樣,不管外語掌握到什麼程度,也不會像母語那樣使用得那麼好,時常也會出一些“拼寫錯誤”問題。

           亂碼的出現根本原因在於編碼和解碼使用了不同的編碼方案。比如用GBK編碼的文件,用UTF-8去解碼結果肯定都是火星文。所以要解決這個問題,中心思想就在於使用統一的編碼方案。

     

    jsp頁面間的參數傳遞有以下幾種方式:1、表單(form)的提交。2、直接使用URL後接參數的形式(超級鏈接)。3、如果兩個jsp頁面在兩個不同的窗口中,並且這兩個窗口是父子的關系,子窗口中的jsp也可以使用javascript和DOM(window.opener.XXX.value)來取得父窗口中的jsp的輸入元素的值。下面就前兩種方式中出現的亂碼問題做一下剖析。

     

    1、表單(form)的提交實現參數頁面間的傳遞

    在介紹表單傳遞參數的內容之前,先來了解一些預備知識。表單的提交方式和請求報文中對漢字的處理。

     

    表單的提交方式:

    通常使用的表單的提交方式主要是:post和get兩種。兩者的區別在於:post方式是把數據內容放在請求的數據正文部分,沒有長度的限制;get方式則是把數據內容直接跟在請求的頭部的URL後面,有長度的限制。下面是同一個頁面兩種方式的請求報許文。

    Requesttest.jsp代碼

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"   
    2.     pageEncoding="UTF-8"%>    
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lo
    4. ose.dtd">    
    5. <html>    
    6. <head>    
    7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
    8. <title>Insert title here</title>    
    9. </head>    
    10. <body>    
    11. <%-- post方式提交表單 --%>    
    12. <form action="http://localhost:8888/EncodingTest/requestresult.jsp" method="post">    
    13.     UserName:<input type="text" name="username"/>    
    14.     Password:<input type="password" name="password"/>    
    15.     <input type="submit" value="Submit">    
    16. </form>    
    17. </body>    
    18. </html>   
    1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!D
    2. OCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loos
    3. e.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <t
    4. itle>Insert title here</title> </head> <body> <%-- post方式提交表單 --%> <form action="http://loc
    5. alhost:8888/EncodingTestb/requestresult.jsp" method="post"> UserName:<input type="text" nam
    6. e="username"/> Password:<input type="password" name="password"/> <input type="submit" va
    7. lue="Submit"> </form> </body> </html>  

    在上面的請求頁面的username輸入框裡輸入的是“世界杯”三個漢字,password輸入框中輸入"123"後按下Submit按鈕提交請求。截獲到的請求報文如下:

    Post方式的請求報文代碼

    1. POST /EncodingTest/requestresult.jsp HTTP/1.1   
    2. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applicati
    3. on/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
    4. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
    5. Accept-Language: zh-cn    
    6. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ci
    7. ba; .NET CLR 2.0.50727)    
    8. Content-Type: application/x-www-form-urlencoded    
    9. Accept-Encoding: gzip, deflate    
    10. Host: localhost:8888   
    11. Content-Length: 49   
    12. Connection: Keep-Alive    
    13. Cache-Control: no-cache    
    14.    
    15. username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123   
    16. POST /EncodingTest/requestresult.jsp HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjp
    17. eg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, applicati
    18. on/msword, */* Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn Us
    19. er-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .N
    20. ET CLR 2.0.50727) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate H
    21. ost: localhost:8888 Content-Length: 49 Connection: Keep-Alive Cache-Control: no-cache username=%E
    22. 4%B8%96%E7%95%8C%E6%9D%AF&password=123  

    以上報文內容,可以看出post方式的請求報文是有專門的數據部的。,

    下面的同一請求頁面的get提交方式的請求報文:

    Get方式的請求報文代碼

    1. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123 H
    2. TTP/1.1   
    3. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applica
    4. tion/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
    5. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
    6. Accept-Language: zh-cn    
    7. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-cib
    8. a; .NET CLR 2.0.50727)    
    9. Accept-Encoding: gzip, deflate    
    10. Host: localhost:8888   
    11. Connection: Keep-Alive   
    12. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&passwo
    13. rd=123 HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockw
    14. ave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Refer
    15. er: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn User-Agent: Mozi
    16. lla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .NET CLR 2.0.50
    17. 727) Accept-Encoding: gzip, deflate Host: localhost:8888 Connection: Keep-Alive  

    以上報文內容,可以看出get方式的請求報文沒有專門的數據部,數據是直接跟在url的後面。

     

    請求報文中對漢字的處理:

    從上面兩種報文可以看出頁面上輸入的“世界杯”三個漢字被替換成了"%E4%B8%96%E7%95%8C%E6%9D%AF”這樣一個字符串,然後發給服務器的。看到這,可能會有兩個問題:問題一、這個字符串是什麼?問題二、為什麼要做這樣的替換?

     

    這個字符串是“世界杯”這三個漢字對應的"UTF-8”編碼"E4B896E7958CE69DAF"在每個字節前追加一個"%"後形成的。至於為什麼要做這樣的轉化,我的理解是:因為請求報文會以"ISO-8859-1"的編碼方式編碼後,通過網絡流的方式傳送到服務器端。"ISO-8859-1"僅支持數字、英文字母和一些特殊字符,所以像漢字等這樣的字符"ISO-8859-1"是不認識的。所以就必須先給這些"ISO-8859-1"不支持的字符做個“整形”手術。這樣才能正確的將頁面上的信息傳送到服務器端。

     

    這時可能又會有另外一個問題:上面的例子中為什麼會選用"UTF-8"編碼,其它的編碼方案可以嗎?答案是可以的。在jsp頁面代碼的頭部有這樣一段代碼"<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>"其中charset的值就是浏覽器在提交請求報文前,對請求報文做“整形”手術時用的字符集,同是也是浏覽器解釋服務器的響應頁面時的字符集。

     

    在了解了以上內容後,開始剖析表單方式傳遞參數的亂碼問題。

    以上例為例,點擊"Submit"按鈕後,浏覽器將做完“整形”手術後的請求報文發送給WEB服務器上的Servlet容器,容器在收到這個請求報文後,會解析這個請求報文並用這個報文的信息生成一個HttpServletRequest對象,然後將這個HttpServletRequest對象傳給這個頁面所要請求的jsp或Servlet(上例中為"requestresult.jsp")。在這個被請求的jsp或Servlet(上例中為"requestresult.jsp")中,使用HttpServletRequest對象的getParameter("")方法來取得上一頁面傳來的參數。默認情況下,這一方法使用的是"ISO-8859-1"來解碼,所以對於英文或數字的參數值自然能正確取得,但對於漢字這樣的字符是解不出來的,因為那幾個漢字曾經做過“整形”手術,已經認不出來了。要想再把它們認出來,那就得要把手術的主刀醫生找到,然後再做一次“還原”手術。下面提供的幾個方案,可用於不同的情況。

     

     

     

     

    方案一代碼

    1. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %>    
    2. Username:<%=str %>   
    3. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %> Usern
    4. ame:<%=str %>  

    既然request.getParameter("username")默認情況下返回的字符串是用"ISO-8859-1"解出來的,那就先把這個不可辨認的字符串再用"ISO-8859-1"來打散,也就是:request.getParameter("username").getBytes("ISO-8859-1")。最後再用跟你的頁面的charset一致的字符集來重組這個字符串:new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8")。這樣就能見到它的廬山真面目了。

    方案一是一種比較萬能的方法,不管是post還是get都適用,但可以看出它的缺點是:對於每個可能出現漢字的參數都要顯示的做這麼一段處理。一個兩個還行,要是很多的話,那就應該考慮一下是不是可以選用下一種方案。

    方案二代碼

    1. <%request.setCharacterEncoding("UTF-8"); %>   
    2. <%request.setCharacterEncoding("UTF-8"); %>  

    方案二是在頁面的最開始或者是在該頁面中使用的第一個request.getParameter("")方法之前加上上述一段代碼,它的作用是用作為參數傳入的編碼集去覆蓋request對象中的默認的"ISO-8859-1"編碼集。這樣request.getParameter("")方法就會用新的編碼集去解碼,因為"UTF-8"支持中文,所以作為參數傳過來的“世界杯”三個漢字就能正確的接收到了。但關於request.setCharacterEncoding("")方法,API文檔中有如下的說明:

     

    Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effectb.

     

     

    所以方案二只對post方式提交的請求有效,因為參數都在request的body區。而對get方式提交的請求則是無效的,這時你會發現同樣的做法但顯示的還是亂碼。所以你的請求要是是以get方式提交的話,那你還是乖乖的選用方案一吧!

     

    從上面的敘述可以知道,方案二需要在每個頁面的前頭加上<%request.setCharacterEncoding("UTF-8"); %>這段代碼,這樣做是不是也挺累的,所以我們想到了使用過濾器來幫助我們做這件事兒,那就清爽、簡單多了。

    Encodingfilter代碼

    1. public class EncodingFilter implements Filter {    
    2.         
    3.     private String charset;    
    4.     @Override    
    5.     public void destroy() {    
    6.         // TODO Auto-generated method stub    
    7.     }    
    8.    
    9.     @Override    
    10.     public void doFilter(ServletRequest request, ServletResponse response,    
    11.             FilterChain chain) throws IOException, ServletException {    
    12.         //用init方法取得的charset覆蓋被攔截下來的request對象的charset    
    13.         request.setCharacterEncoding(this.charset);    
    14.         //將請求移交給下一下過濾器,如果還有的情況下。    
    15.         chain.doFilter(request, response);    
    16.     }    
    17.    
    18.     @Override    
    19.     public void init(FilterConfig config) throws ServletException {    
    20.         //從web.xml中的filter的配制信息中取得字符集    
    21.         this.charset = config.getInitParameter("charset");    
    22.     }    
    23. }   
    24. public class EncodingFilter implements Filter { private String charset; @Override public void destr
    25. oy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req
    26. uest, ServletResponse response, FilterChain chain) throws IOException, ServletException { //用init方
    27. 法取得的charset覆蓋被攔截下來的request對象的charset request.setCharacterEncoding(this.charset); //將
    28. 請求移交給下一下過濾器,如果還有的情況下。 chain.doFilter(request, response); } @Override pub
    29. lic void init(FilterConfig config) throws ServletException { //從web.xml中的filter的配制信息中取得字
    30. 符集 this.charset = config.getInitParameter("charset"); } }  

    要想這個過濾器生效,還得到web.xml裡加入下面的配制信息。

    Web.xml代碼

    1. <filter>    
    2.    <filter-name>EncodingFilter</filter-name>    
    3.    <filter-class>cn.eric.encodingtest.filter.EncodingFilter</filter-class>    
    4.    <init-param>    
    5.        <param-name>charset</param-name>    
    6.        <param-value>UTF-8</param-value>    
    7.    </init-param>    
    8. </filter>    
    9. <filter-mapping>    
    10.    <filter-name>EncodingFilter</filter-name>    
    11.    <url-pattern>/*</url-pattern>    
    12. </filter-mapping>   
    13. <filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.eric.encodingtest.filter.Encodi
    14. ngFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UT
    15. F-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filt
    16. er-name> <url-pattern>/*</url-pattern> </filter-mapping>  

    2、直接使用URL後接參數的形式(超級鏈接)。

    有些時候可能會遇到通過一個超級鏈接來把參數傳到下一個頁面,而剛好這個參數的值有可能會出現中文的情況。就像下面這樣:

    1. <a href="./jstlresult.jsp?content=世界杯">Go South Africa 

    跟form提交有些不同的是:當你點擊這個超級鏈接後在浏覽器的地址欄裡看到的是http://localhost:8080/TomcatJndiTest/jstlresult.jsp?content=世界杯,而不是http://localhost:8080/TomcatJndiTest/jstlresult.jsp?content=%E4%B8%96%E7%95%8C%E6%9D%AF

    這裡浏覽器並沒有幫我們把這個轉化工作搞定,所以這裡要自己動手,豐衣足食了。做法如下:

    1. <a href="./jstlresult.jsp?content=<%=java.net.URLEncoder.encode("世界杯","utf-8") %>">Go South Africa 

    這樣的話在第二個頁面就能使用

    1. <%String str = new String(request.getParameter("content").getBytes("ISO-8859-1"),"utf-8"); %> 

    的方法來正確的得到這個參數值了。

     

    總結一下:

    1、post提交的方式:使用過濾器,將到達頁面前的request對象中的字符編碼設定成跟你頁面統一的編碼。

    2、get提交的方式:<%String str = new String(request.getParameter("content").getBytes("ISO-8859-1"),"utf-8"); %>這樣的字符串重組的方法。

    3、超級鏈接方式:先將鏈接url中的漢字用java.net.URLEncoder.encode("paramValue","charset")方法處理一下,下面的做法參照2。

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved