java url中文參數亂碼問題

http://www.blogjava.net/jerry-zhaoj/archive/2009/07/16/286993.html 轉html

JAVA 中URL連接中文參數亂碼的處理方法JAVA 中URL連接中文參數亂碼的處理方法

JAVA 中URL連接中文參數亂碼的處理方法

在url中問參數亂碼,咱們已經在 "解決ajax提交中文、URL中文參數傳遞後的亂碼問題的解決辦法" 一張提出了一中解決辦法, 網子是:http://www.javaly.cn/javahome/java/content.detail/%E8%A7%A3%E5%86 %B3ajax%E6%8F%90%E4%BA%A4%E4%B8%AD%E6%96%87%E3%80%81URL%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92%E5%90%8E%E7%9A%84%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/fdc1ed6c22069c56012282a321ed0465.faces

 今天,咱們找出其餘的解決辦法。
java

            

方法一:

            http://xxx.do?ptname='我是中國人'
            String strPtname = request.getParameter("ptname");
            strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");  ajax

方法二:

            

<%@ page contentType="text/html;charset=gb2312" %>jsp

            

<a href="ds.jsp?url=<%=java.net.URLEncoder.encode("編碼的是這裏","GB2312")%>">點擊這裏</a>函數

            


            <%
            //request.setCharacterEncoding("GBK");
            if(request.getParameter("url")!=null)
            {post

            

str=request.getParameter("url");
            str=java.net.URLDecoder.decode(str,"GB2312");
            str=new String(str.getBytes("ISO-8859-1"));
            out.print(str);
            }編碼

            

%>url

            

==================================spa

            

public String chinatoString(String str)
                  {
                   String s=str;
                   try
                   {
                    byte tempB[]=s.getBytes("ISO-8859-1");
                    s=new String(tempB);
                    return s;
                   }
                   catch(Exception e)
                   {
                    return s;
                   }
                 }.net

            

====================================================

            

function URLencode(sStr) 
                 {
                   return escape(sStr).
                            replace(/"+/g, '%2B').
                               replace(/""/g,'%22').
                                  replace(/"'/g, '%27').
                                    replace(/"//g,'%2F');
                 }

            

 

            方法三:

            若是用jstl的話,能夠本身寫一個el的function,調用URLEncoder.encode來編碼。

            

IE缺省對URL後面的參數是不編碼發送的,可是tomat缺省是按ISO8859-1來進行URL解碼,所以纔會出現上述錯誤。好的作法是: 
            一、在URL參數中確保用UTF-8編碼之,方法能夠用js函數encodeURI(),或調用自定義的el function; 
            二、設置server.xml中的Connector熟悉URIEncoding="UTF-8",確保解碼格式與編碼格式統一;

方法四:

            

<script>             for(var i=0;i<document.links.length;i++){             document.links[i].href=encodeURI(document.links[i].href);             }             </script>             在action中,String s=request.getParameter("s");             s=new String(s.getBytes("iso-8859-1"),"gbk");

相關文章
相關標籤/搜索