【Web後端筆記】jsp傳遞中文資料出現亂碼的問題

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   使用   

JSP檔案中亂碼現象經常遇到,那如何處理就是我們不可避免的話題,那麼該如何解決呢,下面我針對不同的情況,給出幾種處理方式: 

1) JSP頁面中文亂碼,這種情況比較好處理,在頁面的page指令中加上如下一項就行
<%@ page contentType=”text/html;charset=UTF-8” %> 
2) JSP頁面採用表單提交時,提交的資料中包含中文,並且提交方式為post這時我們擷取表單資料後,展示到其它頁面時會出現亂碼,解決方案是在擷取請求中的參數前,先佈建要求的字元集。
1 request.setCharacterEncoding(“UTF-8”) ; 
3) 還是表單提交資料,但是提交方式為預設的get方式,這時即使我們設定了請求字元集,但取出來還是亂碼,這時需要如下的轉換方式,我把轉換方式用一個編碼轉換類來實現了。
 1 /*  2 * 定義一個轉換字元集的工具類CharSetTool  3 *  4 * 定義一個方法,用來轉換  5 */  6 public class CharSetTool  7 {  8 /*  9 * 方法toUTF8() 10 * 參數: @inStr 傳入要轉換的字串 11 */ 12 public static String toUTF8(String inStr) 13 { 14 String rtStr=""; 15 if(inStr!=null) 16 { 17 try 18 { 19 rtStr=new String(inStr.getBytes("ISO-8859-1"),"UTF-8"); 20 } 21 catch (UnsupportedEncodingException e) 22 { 23 //列印錯誤資訊 24 System.out.println(e.getMessage()); 25 } 26 } 27 return rtStr; 28 } 29 } 
4) URL中文參數亂碼處理, 
比如:超連結方式
1 <% String str="我是中國人"; %> 2 <a href="next?str=<%=str%>">我是連結,單擊可以提交參數</a> 
註:next 為 Servlet的URL Pattern 

那在將中文作為參數值傳遞時,首先要把中文字串進行編碼轉換,轉換成URL格式
操作如下:
1 <% String str="我是中國人"; 2 //使用URLEncoder.encode方法 3 str = URLEncoder.encode(str,"UTF-8"); 4 %> 5 <a href="next?str=<%=str%>">我是連結,單擊可以提交參數</a> 
綜合以上幾種比較常見的JSP中文亂碼的處理方式,不管是何種提交方式,我們都可以使用JSP中的過濾器(Filter)來過濾字元編碼,下面我就給出一個我自己定義的 “通用”的字集編碼過濾器。
 1 /**  2 * 處理中文字元集的過濾器  3 *  4 * @author Administrator  5 *  6 */  7 public class EncodingFilter implements Filter  8 {  9 //過濾器銷毀的方法 10 public void destroy() 11 { 12 } 13 14 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chan) 15 throws IOException, ServletException 16 { 17 18 //設定響應模式 19 response.setContentType("text/html;charset=UTF-8"); 20 21 //判斷用戶端的提交方式 22 if(((HttpServletRequest)request).getMethod().equalsIgnoreCase("POST")) 23 { 24 //POST方式提交 25 //佈建要求字元集 26 request.setCharacterEncoding("UTF-8"); 27 28 } 29 else 30 { 31 //處理GET 32 //擷取所有的提交參數 33 Enumeration names = request.getParameterNames(); 34 35 //遍曆 36 while(names.hasMoreElements()) 37 { 38 //參數名 39 String name = (String)names.nextElement(); 40 41 //因為不知道參數是多值還是單值 42 String values[] = request.getParameterValues(name); 43 44 //迴圈處理字元集轉換 45 for(int i=0;i<values.length;i++) 46 { 47 values[i] = toUTF8(values[i]); 48 } 49 } 50 } 51 //調用過濾器鏈中的下一個過濾器 52 chan.doFilter(request, response); 53 } 54 55 //轉換方法(內部私人方法) 56 private String toUTF8(String str) 57 { 58 String rt = null; 59 60 try { 61 if(str!=null) 62 { 63 //轉換新字元集 64 rt = new String(str.getBytes("ISO-8859-1"),"UTF-8"); 65 66 } 67 } catch (UnsupportedEncodingException e) { 68 // TODO 自動產生 catch 塊 69 e.printStackTrace(); 70 } 71 72 return rt; 73 } 74 75 //初始化方法 76 public void init(FilterConfig config) throws ServletException 77 { 78 } 79 } 
然後在web.xml檔案中進行相關的配置 : 
 1 <!-- 配置過濾器 -->  2 <filter>  3 <filter-name>EncodingFilter</filter-name>  4 <filter-class>ocean.mvc.filter.EncodingFilter</filter-class>  5 </filter>  6  7 <filter-mapping>  8 <filter-name>EncodingFilter</filter-name>  9 <url-pattern>/*</url-pattern> <!-- 匹配所有的請求 --> 10 </filter-mapping>

參考原文:http://zhidao.baidu.com/link?url=8S3bNfWgwzTSRGeC4eO2SbBgSJShV9r_wpUOlCVKNcn2ERb40A0g5AjSro3aaRaqStuysTa1OvzroMoN0-t81_



 
 

【Web後端筆記】jsp傳遞中文資料出現亂碼的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.