解决jsp 的URI 中的中文传参问题

Java代码 复制代码
  1. <%! public String GBToISO(String str)      
  2. {try{byte temp[]=str.getBytes("GB2312");      
  3. str=new String(temp,"ISO-8859-1");      
  4. return str;      
  5. }catch(Exception e){return str;}}      
  6.      
  7. response.sendRedirect(GBToISO("errmsg.<SPAN class=hilite1>jsp</SPAN>?errmsg=添加客户信息成功"));%>    
<%! public String GBToISO(String str)   
{try{byte temp[]=str.getBytes("GB2312");   
str=new String(temp,"ISO-8859-1");   
return str;   
}catch(Exception e){return str;}}   
  
response.sendRedirect(GBToISO("errmsg.jsp?errmsg=添加客户信息成功"));%>  


超连接中profession为中文

Java代码 复制代码
  1. <a href="cust_totallist.<SPAN class=hilite1>jsp</SPAN>?action=delete&page=<%=intCurrentPage%>&cust_id=<%=rs.getInt("id")%>&profession=<%=java.net.<SPAN class=hilite2>URL</SPAN>Encoder.encode(profession,"ISO-8859-1")%>">删除</a>      
  2. ////////////cust_totallist.<SPAN class=hilite1>jsp</SPAN>中取profession值      
  3. String profession=java.net.<SPAN class=hilite2>URL</SPAN>Decoder.decode(request.getParameter("profession").trim(),"ISO-8859-1");    
<a href="cust_totallist.jsp?action=delete&page=<%=intCurrentPage%>&cust_id=<%=rs.getInt("id")%>&profession=<%=java.net.URLEncoder.encode(profession,"ISO-8859-1")%>">删除</a>   
////////////cust_totallist.jsp中取profession值   
String profession=java.net.URLDecoder.decode(request.getParameter("profession").trim(),"ISO-8859-1");  


可见,URL中编码格式为ISO-8859-1,处理中文只需将编码格式转换ISO-8859-1
方法一:
http://xxx.do?ptname='我是中国人'

Java代码 复制代码
  1. String strPtname = request.getParameter("ptname");    
  2. strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");   
String strPtname = request.getParameter("ptname"); 
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8"); 


方法二:

Java代码 复制代码
  1. <%@ page contentType="text/html;charset=gb2312" %>    
  2. <a href="ds.<SPAN class=hilite1>jsp</SPAN>?<SPAN class=hilite2>url</SPAN>=<%=java.net.<SPAN class=hilite2>URL</SPAN>Encoder.encode("编码的是这里","GB2312")%>">点击这里</a>    
  3. <%    
  4. //request.setCharacterEncoding("GBK");    
  5. if(request.getParameter("<SPAN class=hilite2>url</SPAN>")!=null)    
  6. {    
  7. str=request.getParameter("<SPAN class=hilite2>url</SPAN>");    
  8. str=java.net.<SPAN class=hilite2>URL</SPAN>Decoder.decode(str,"GB2312");    
  9. str=new String(str.getBytes("ISO-8859-1"));    
  10. out.print(str);    
  11. }    
  12. %>   
<%@ 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); 
} 
%> 
Java代码 复制代码
  1. public String chinatoString(String str)    
  2. {    
  3. String s=str;    
  4. try    
  5. {    
  6. byte tempB[]=s.getBytes("ISO-8859-1");    
  7. s=new String(tempB);    
  8. return s;    
  9. }    
  10. catch(Exception e)    
  11. {    
  12. return s;    
  13. }    
  14. }   
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; 
} 
} 
Java代码 复制代码
  1. function <SPAN class=hilite2>URL</SPAN>encode(sStr)    
  2. {    
  3. return escape(sStr).    
  4. replace(/\+/g, '%2B').    
  5. replace(/\"/g,'%22').    
  6. replace(/\'/g, '%27').    
  7. replace(/\//g,'%2F');    
  8. }   
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",确保解码格式与编码格式统一;

方法四:

Xml代码 复制代码
  1. <script>    
  2. for(var i=0;i<document.links.length;i++){    
  3. document.links[i].href=encodeURI(document.links[i].href);    
  4. }    
  5. </script>   
<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");

以上方法是收聚了一些网友所讲的解决方法

原文地址:https://www.cnblogs.com/cy163/p/1299068.html