有一枚學生問問了我一個問題,突然靈感爆發,他使用的Spring的過濾器,前台利用GET方式向后端發出一個請求,由於里面含有中文數據,結果在后端顯示的是亂碼,他問我為什么?明明在Spring里面也配了字符過濾器,卻出現了亂碼,所以就看了一下spring實現的該過濾器,下面是過濾器的實現代碼org.springframework.web.filter.CharacterEncodingFilter.java
public class CharacterEncodingFilter extends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; /** * Set the encoding to use for requests. This encoding will be passed into a * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. * <p>Whether this encoding will override existing request encodings * (and whether it will be applied as default response encoding as well) * depends on the {@link #setForceEncoding "forceEncoding"} flag. */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Set whether the configured {@link #setEncoding encoding} of this filter * is supposed to override existing request and response encodings. * <p>Default is "false", i.e. do not modify the encoding if * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} * returns a non-null value. Switch this to "true" to enforce the specified * encoding in any case, applying it as default response encoding as well. */ public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); //此處設置是處理POST方式的編碼參數問題 if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); } }
在web.xml該過濾器是這樣配置的:需要設置的兩個參數為encoding、forceEncoding,分別設置字符集及是否設置字符集,該filter也非常簡單
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
有的時候,看到源碼才知道一些真理,所有才知道spring只是利用request.setCharacterEncoding(this.encoding);幫助我們處理了POST方式的亂碼問題,碰到GET方式的提交,還是會出現亂碼。
注意:自從Tomcat5.x開始,就對GET方式和POST方式的提交分別給予不同的處理方式[所以在二階段學習的時候就應該嚴格區分get和post請求的處理情況,養成良好的習慣,想想是否做的到]。POST方式是利用request.setCharacterEncoding()來進行設置編碼,如果沒有設置的話,就是按照默認的ISO-8859-1來進行編碼;GET方式提交總是利用默認的ISO-8859-1來進行編碼參數。
中文亂碼解決方案
1.利用String[也是最常用的方式]
String username = new String(username.getBytes("ISO-8859-1"), "UTF-8"); //通過默認的編碼獲取到byte[],然后進行UTF-8再次編碼
2.在tomcat中的server.xml進行配置URIEncoding="UTF-8"
<Connector URIEncoding="UTF-8" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
3.使用JavaScript對傳遞的參數進行編碼
額外閱讀
Js編碼的幾種方式區別: 1.window.escape()與HttpUtility.UrlEncodeUnicode()編碼格式一樣:將一個漢字編碼為%uxxxx格式
2.window.encodeURIComponent()[我推薦使用這種方式]與HttpUtility.UrlEncode()編碼格式一樣:將一個漢字編碼為%xx%xx%xx的格式 不會被window.encodeURIComponent編碼的字符有:' ( ) * - . _ ! ~ 這與http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp解釋相符合 不會被HttpUtility.UrlEncode編碼的字符有:' ( ) * - . _ ! 相比較而言,HttpUtility.UrlEncode比window.encodeURIComponent多一個 ~ 編碼
3.不會被window.encodeURI編碼的字符有: - _ . ! * ( ) ; / ? : @ & = $ , #,與encodeURIComponent對比,發現encodeURI不對:;/?:@&=+$,#這些用於分隔 URI 組件的標點符號進行編碼 |
第一種: 事例演示說明 JavaScript代碼: window.self.location="searchbytext.action?searchtext="+encodeURIComponent(encodeURIComponent(seartext)); java后台處理代碼: searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8"); /* 為什么要兩次編碼的原因:后台java代碼給searchtext賦值的時候,本身已經使用了一次解碼,不過解碼的結果依然不對。所以我們可以在頁面上進行兩次編碼操作,這樣后台自動的那次就可以抵消掉一次,然后在使用searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8");進行一次解碼就好了。【這種方式還是用的比較多的,我個人使用的比較少】 */
我個人來說還是比較喜歡第一種情況,但是有的時候服務器有兼容問題,我曾經就遇到在tomcat中好用,放置到Weblogic后不好使了,之后我就只用第三種方式,對於第三種方式我不過多解釋,希望大家有印象
我寫了這么多,為什么你們總是不回復我呢!<( ̄▽ ̄)> 哇哈哈…!