JAVA 中URL链接中文参数乱码

方法一:

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 中URL链接中文参数乱码的若干处理方法,现在整理收录如下:

方法一:

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");


js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                           

例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape
[Huoho.Com编辑]

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。


最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

posted @ 2011-01-26 17:10  琥珀光  阅读(6817)  评论(0编辑  收藏  举报