jsp中URL链接中文参数乱码怎么解决



jsp中URL链接中文参数乱码怎么解决?JAVA 中URL链接中文参数乱码的若干处理方法,现在整理收录如下:

jspURL链接中文乱码解决方法一:

http://xxx.do?ptname=’我是中国人’

String strPtname = request.getParameter(“ptname”);

strPtname = new String(strPtname.getBytes(“ISO-885Array-1″), “UTF-8″);

jspURL链接中文乱码解决方法二:

<%@ 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-885Array-1″));

out.print(str);

}

%>

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

public String chinatoString(String str)

{

String s=str;

try

{

byte tempB[]=s.getBytes(“ISO-885Array-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’);

}

jspURL链接中文乱码解决方法三:

加入是用jstl的话,自己就写一个el的function,调用URLEncoder.encode来编码。IE浏览器缺默认URL后面的参数是不编码发送,而tomat默认是按ISO885Array-1来进行URL解码,所以才会出现上述错误。好的做法是:

1、在URL参数中确保用UTF-8编码之,方法可以用js函数encodeURI(),或调用自定义的el function;

2、设置server.xml中的Connector熟悉URIEncoding=”UTF-8″,确保解码格式与编码格式统一;

jspURL链接中文乱码解决方法四:

<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-885Array-1″),”gbk”);