有一枚學生問問了我一個問題,突然靈感爆發,他使用的Spring的過濾器,前臺利用GET方式向后端發出一個請求,由于里面含有中文數據,結果在后端顯示的是亂碼,他問我為什么?明明在Spring里面也配了字符過濾器,卻出現了亂碼,所以就看了一下spring實現的該過濾器,下面是過濾器的實現代碼org.springframework.web.filter.CharacterEncodingFilter.java
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
/**
* Servlet Filter that allows one to specify a character encoding for requests.
* This is useful because current browsers typically do not set a character
* encoding even if specified in the HTML page or form.
*
* <p>This filter can either apply its encoding if the request does not already
* specify an encoding, or enforce this filter's encoding in any case
* ("forceEncoding"="true"). In the latter case, the encoding will also be
* applied as default response encoding (although this will usually be overridden
* by a full content type set in the view).
*
* @author Juergen Hoeller
* @since 15.03.2004
* @see #setEncoding
* @see #setForceEncoding
* @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
* @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
*/
public class CharacterEncodingFilter extends OncePerRequestFilter {
private String encoding;
private boolean forceRequestEncoding = false;
private boolean forceResponseEncoding = false;
/**
* Create a default {@code CharacterEncodingFilter},
* with the encoding to be set via {@link #setEncoding}.
* @see #setEncoding
*/
public CharacterEncodingFilter() {
}
/**
* Create a {@code CharacterEncodingFilter} for the given encoding.
* @param encoding the encoding to apply
* @since 4.2.3
* @see #setEncoding
*/
public CharacterEncodingFilter(String encoding) {
this(encoding, false);
}
/**
* Create a {@code CharacterEncodingFilter} for the given encoding.
* @param encoding the encoding to apply
* @param forceEncoding whether the specified encoding is supposed to
* override existing request and response encodings
* @since 4.2.3
* @see #setEncoding
* @see #setForceEncoding
*/
public CharacterEncodingFilter(String encoding, boolean forceEncoding) {
this(encoding, forceEncoding, forceEncoding);
}
/**
* Create a {@code CharacterEncodingFilter} for the given encoding.
* @param encoding the encoding to apply
* @param forceRequestEncoding whether the specified encoding is supposed to
* override existing request encodings
* @param forceResponseEncoding whether the specified encoding is supposed to
* override existing response encodings
* @since 4.3
* @see #setEncoding
* @see #setForceRequestEncoding(boolean)
* @see #setForceResponseEncoding(boolean)
*/
public CharacterEncodingFilter(String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding) {
Assert.hasLength(encoding, "Encoding must not be empty");
this.encoding = encoding;
this.forceRequestEncoding = forceRequestEncoding;
this.forceResponseEncoding = forceResponseEncoding;
}
/**
* 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;
}
/**
* Return the configured encoding for requests and/or responses
* @since 4.3
*/
public String getEncoding() {
return this.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.
* <p>This is the equivalent to setting both {@link #setForceRequestEncoding(boolean)}
* and {@link #setForceResponseEncoding(boolean)}.
* @see #setForceRequestEncoding(boolean)
* @see #setForceResponseEncoding(boolean)
*/
public void setForceEncoding(boolean forceEncoding) {
this.forceRequestEncoding = forceEncoding;
this.forceResponseEncoding = forceEncoding;
}
/**
* Set whether the configured {@link #setEncoding encoding} of this filter
* is supposed to override existing request 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.
* @since 4.3
*/
public void setForceRequestEncoding(boolean forceRequestEncoding) {
this.forceRequestEncoding = forceRequestEncoding;
}
/**
* Return whether the encoding should be forced on requests
* @since 4.3
*/
public boolean isForceRequestEncoding() {
return this.forceRequestEncoding;
}
/**
* Set whether the configured {@link #setEncoding encoding} of this filter
* is supposed to override existing response encodings.
* <p>Default is "false", i.e. do not modify the encoding.
* Switch this to "true" to enforce the specified encoding
* for responses in any case.
* @since 4.3
*/
public void setForceResponseEncoding(boolean forceResponseEncoding) {
this.forceResponseEncoding = forceResponseEncoding;
}
/**
* Return whether the encoding should be forced on responses.
* @since 4.3
*/
public boolean isForceResponseEncoding() {
return this.forceResponseEncoding;
}
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String encoding = getEncoding();
if (encoding != null) {
if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(encoding);//解決POST請求中文亂碼問題
}
if (isForceResponseEncoding()) {
response.setCharacterEncoding(encoding);
}
}
filterChain.doFilter(request, response);
}
}
在web.xml該過濾器是這樣配置的:需要設置的兩個參數為encoding、forceEncoding,分別設置字符集及是否設置字符集,該filter也非常簡單
<!-- 解決POST請求的中文亂碼問題 -->
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
有的時候,看到源碼才知道一些真理,所有才知道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[也是最常用的方式]--查閱JDK API
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" />
增加屬性 URIEncoding="UTF-8" 一勞永逸解決GET請求的亂碼問題
3.使用JavaScript對傳遞的參數進行編碼
Js編碼的幾種方式區別:
1.window.escape()與HttpUtility.UrlEncodeUnicode()編碼格式一樣:將一個漢字編碼為%uxxxx格式
不會被window.escape編碼的字符有:@ _ - . * / + 這與http://www.w3school.com.cn/js/jsref_escape.asp上的解釋不符合
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不對:;/?:@&=+, #,與encodeURIComponent對比,發現encodeURI不對:;/?:@&=+,#這些用于分隔 URI 組件的標點符號進行編碼
window.encodeURIComponent() 推薦方式
事例演示說明
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");
進行一次解碼就好了?!具@種方式還是用的比較多的,我個人使用的比較少】
*/