Java代碼
- <%! 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.<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代碼
- <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>
- ////////////cust_totallist.<SPAN class=hilite1>jsp</SPAN>中取profession值
- 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代碼
- String strPtname = request.getParameter("ptname");
- 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代碼
- <%@ page contentType="text/html;charset=gb2312" %>
- <a href="ds.<SPAN class=hilite1>jsp</SPAN>?<SPAN class=hilite2>url</SPAN>=<%=java.net.<SPAN class=hilite2>URL</SPAN>Encoder.encode("編碼的是這裡","GB2312")%>">點擊這裡</a>
- <%
- //request.setCharacterEncoding("GBK");
- if(request.getParameter("<SPAN class=hilite2>url</SPAN>")!=null)
- {
- str=request.getParameter("<SPAN class=hilite2>url</SPAN>");
- str=java.net.<SPAN class=hilite2>URL</SPAN>Decoder.decode(str,"GB2312");
- str=new String(str.getBytes("ISO-8859-1"));
- out.print(str);
- }
- %>
<%@ 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代碼
- 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;
- }
- }
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代碼
- function <SPAN class=hilite2>URL</SPAN>encode(sStr)
- {
- return escape(sStr).
- replace(/\+/g, '%2B').
- replace(/\"/g,'%22').
- replace(/\'/g, '%27').
- replace(/\//g,'%2F');
- }
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代碼
- <script>
- for(var i=0;i<document.links.length;i++){
- document.links[i].href=encodeURI(document.links[i].href);
- }
- </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");
以上方法是收聚了一些網友所講的解決方案