java url中文参数乱码问题

http://www.blogjava.net/jerry-zhaoj/archive/2009/07/16/286993.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

 今天,我们找出其他的解决办法。

            

方法一:

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

方法二:

            

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

            

<a href="ds.jsp?url=<%=java.net.URLEncoder.encode("编码的是这里","GB2312")%>">点击这里</a>

            


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

            

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

            

%>

            

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

            

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;
                   }
                 }

            

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

            

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解码,因此才会出现上述错误。好的做法是: 
            1、在URL参数中确保用UTF-8编码之,方法可以用js函数encodeURI(),或调用自定义的el function; 
            2、设置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");

你可能感兴趣的:(java)