url中文亂碼解決大全

使用 tomcat 時,相信你們都回遇到中文亂碼的問題,具體表現爲經過表單取得的中文數據爲亂碼。javascript

1、初級解決方法 
經過一番檢索後,許多人採用了以下辦法,首先對取得字符串按照 iso8859-1 進行解碼轉換,而後再按照 gb2312 進行編碼,最後獲得正確的內容。示例代碼以下:
java

http://xxx.do?ptname='我是中國人'web

String strPtname = request.getParameter("ptname");tomcat

strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");
String para = new String( request.getParameter("para").getBytes("iso8859-1"), "gb2312"); 
具體的緣由是由於美國人在寫 tomcat 時默認使用 iso8859-1 進行編碼形成的。 
然而,在咱們的 servlet 和 jsp 頁面中有大量的參數須要進行傳遞,這樣轉換的話會帶來大量的轉換代碼,很是不便。 
2、入門級解決方法 
後來,你們開始寫一個過濾器,在取得客戶端傳過來的參數以前,經過過濾器首先將取得的參數編碼設定爲 gb2312 ,而後就能夠直接使用 getParameter 取得正確的參數了。這個過濾器在 tomcat 的示例代碼 
jsp-examples 中有詳細的使用示例, 其中過濾器在 web.xml 中的設定以下,示例中使用的是日文的編碼,咱們只要修改成 gb2312 便可 
view plaincopy to clipboardprint?
<filter>    
<filter-name>Set Character Encoding</filter-name>    
<filter-class>filters.SetCharacterEncodingFilter</filter-class>    
<init-param>    
<param-name>encoding</param-name>    
<param-value>EUC_JP</param-value>    
</init-param>    
</filter>   
<filter> 
<filter-name>Set Character Encoding</filter-name> 
<filter-class>filters.SetCharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>EUC_JP</param-value> 
</init-param> 
</filter> 
 
服務器

過濾器的代碼以下: 
view plaincopy to clipboardprint?
public class SetCharacterEncodingFilter implements Filter {    
// 編碼的字符串    
protected String encoding = null;    
// 過濾器的配置    
protected FilterConfig filterConfig = null;    
// 是否忽略客戶端的編碼    
protected boolean ignore = true;    
// 銷燬過濾器    
public void destroy() {    
this.encoding = null;    
this.filterConfig = null;    
}    
// 過濾方法    
public void doFilter(ServletRequest request, ServletResponse response,    
FilterChain chain)    
throws IOException, ServletException {    
// 若是使用過濾器,忽略客戶端的編碼,那麼使用經過過濾器設定編碼    
if (ignore || (request.getCharacterEncoding() == null)) {    
String encoding = selectEncoding(request);    
if (encoding != null)    
request.setCharacterEncoding(encoding);    
}    
// 傳送給下一個過濾器    
chain.doFilter(request, response);    
}    
  
// 初始化過濾器    
public void init(FilterConfig filterConfig) throws ServletException {    
this.filterConfig = filterConfig;    
this.encoding = filterConfig.getInitParameter("encoding");    
String value = filterConfig.getInitParameter("ignore");    
if (value == null)    
this.ignore = true;    
else if (value.equalsIgnoreCase("true"))    
this.ignore = true;    
else if (value.equalsIgnoreCase("yes"))    
this.ignore = true;    
else    
this.ignore = false;    
}    
// 返回過濾器設定的編碼    
protected String selectEncoding(ServletRequest request) {    
return (this.encoding);    
}    
}   
public class SetCharacterEncodingFilter implements Filter { 
// 編碼的字符串 
protected String encoding = null; 
// 過濾器的配置 
protected FilterConfig filterConfig = null; 
// 是否忽略客戶端的編碼 
protected boolean ignore = true; 
// 銷燬過濾器 
public void destroy() { 
this.encoding = null; 
this.filterConfig = null; 

// 過濾方法 
public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) 
throws IOException, ServletException { 
// 若是使用過濾器,忽略客戶端的編碼,那麼使用經過過濾器設定編碼 
if (ignore || (request.getCharacterEncoding() == null)) { 
String encoding = selectEncoding(request); 
if (encoding != null) 
request.setCharacterEncoding(encoding); 

// 傳送給下一個過濾器 
chain.doFilter(request, response); 
}
jsp

// 初始化過濾器 
public void init(FilterConfig filterConfig) throws ServletException { 
this.filterConfig = filterConfig; 
this.encoding = filterConfig.getInitParameter("encoding"); 
String value = filterConfig.getInitParameter("ignore"); 
if (value == null) 
this.ignore = true; 
else if (value.equalsIgnoreCase("true")) 
this.ignore = true; 
else if (value.equalsIgnoreCase("yes")) 
this.ignore = true; 
else 
this.ignore = false; 

// 返回過濾器設定的編碼 
protected String selectEncoding(ServletRequest request) { 
return (this.encoding); 


 
函數

然而在 tomcat5 中,即便使用過濾器,仍然可能取得亂碼,緣由何在呢? 
3、高級解決方法 
這是由於,在 tomcat4 和 tomcat5 中對參數的處理是不同的,在 tomcat4 中 get 與 post 的編碼是同樣的,因此只要在過濾器中經過 request.setCharacterEncoding 設定一次就能夠解決 get 與 post 的問題。然而,在 tomcat5 中,get 與 post 的處理是分開進行的 
在 tomcat 5 中,爲了解決編碼問題,tomcat 的做者做了不少努力,具體表現爲在 tomcat 的配置文件 server.xml 中對 Connector 元素增長了以下的配置參數,專門用來對編碼進行直接的配置 
URIEncoding 用來設定經過 URI 傳遞的內容使用的編碼,tomcat 將使用這裏指定的編碼對客戶端傳送的內容進行編碼。 
什麼是 URI 呢? 
java doc 的說明中以下說明:URI 是統一資源標識符,而 URL 是統一資源定位符。所以,籠統地說,每一個 URL 都是 URI,但不必定每一個 URI 都是 URL。這是由於 URI 還包括一個子類,即統一資源名稱 (URN),它命名資源但不指定如何定位資源。 
也就是說,咱們經過 get 方法提交的參數實際上都是經過 uri 提交的,都由這個參數管理,若是沒有設定這個參數,則 tomcat 將使用默認的 iso8859-1 對客戶端的內容進行編碼。 
useBodyEncodingForURI 使用與 Body 同樣的編碼來處理 URI, 這個設定是爲了與 tomcat4保持兼容,原來在 tomcat4 和 tomcat5 中隊參數的處理是不同的,在 tomcat4 中 get 與 post 的編碼是同樣的,因此只要在過濾器中經過 request.setCharacterEncoding 設定一次就能夠解決 get 與 post 的問題。然而,在 tomcat5 中,get 與 post 的處理是分開進行的,對 get 的處理經過 前面的 URIEncoding 進行處理,對 post 的內容依然經過 request.setCharacterEncoding 處理,爲了保持兼容,就有了這個設定。 
將 useBodyEncodingForURI 設定爲真後,就能夠經過 request.setCharacterEncoding 直接解決 get 和 post 中的亂碼問題。 
這樣,咱們能夠經過在 server.xml 中設定 URIEncoding 來解決 get 方法中的參數問題,使用過濾器來解決 post 方法中的問題。 
或者也能夠經過在 server.xml 中設定 useBodyEncodingForURI 爲 true ,配合過濾器來解決編碼的問題。 
在這裏,我強烈建議在網站的創做過程當中,全程使用 utf-8 編碼來完全解決亂碼問題。 
具體操做以下: 
一、頁面內容使用 utf-8 格式保存,在頁面中加入 <mete http-equiv="contentType" content="textml;charst=utf-8"> 
二、服務器端的 server.xml 中設定 useBodyEncodingForURI = true 
三、使用過濾器,過濾器設定編碼爲 utf-8
post

四:若是有一些轉碼也轉不過來的話,但是試試打開tomcat的server.xml,找到網站

<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="80" redirectPort="8443">  ui

並在最後加上useBodyEncodingForURI="true" URIEncoding="UTF-8",以下

<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="80" redirectPort="8443"  useBodyEncodingForURI="true" URIEncoding="UTF-8">  

 五:

若是用jstl的話,能夠本身寫一個el的function,調用URLEncoder.encode來編碼。

IE缺省對URL後面的參數是不編碼發送的,可是tomat缺省是按ISO8859-1來進行URL解碼,所以纔會出現上述錯誤。好的作法是:

一、在URL參數中確保用UTF-8編碼之,方法能夠用js函數encodeURI(),或調用自定義的el function;

二、設置server.xml中的Connector熟悉URIEncoding="UTF-8",確保解碼格式與編碼格式統一;

方法四:

 view plaincopy to clipboardprint?
<mce:script type="text/javascript"><!--   
for(var i=0;i<document.links.length;i++){   
  
document.links[i].href=encodeURI(document.links[i].href);   
  
}   
// --></mce:script>  
<mce:script type="text/javascript"><!--
for(var i=0;i<document.links.length;i++){

document.links[i].href=encodeURI(document.links[i].href);

}
// --></mce:script>
 

在action中,String s=request.getParameter("s");

s=new String(s.getBytes("iso-8859-1"),"gbk");

六:js的亂碼解決

1.客戶端:
url=encodeURI(url);
服務器:
String linename = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

2.客戶端:
url=encodeURI(encodeURI(url)); //用了2次encodeURI 
服務器:
String linename = request.getParameter(name);
//java  : 字符解碼
linename = java.net.URLDecoder.decode(linename , "UTF-8");

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/jinxinxin1314/archive/2009/08/16/4453390.aspx

相關文章
相關標籤/搜索