這篇“tomcat GET請求傳參亂碼問題怎么解決”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“tomcat GET請求傳參亂碼問題怎么解決”文章吧。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、蓋州網(wǎng)站維護(hù)、網(wǎng)站推廣。
在Tomcat的通道(Connector)配置中,除了URIEncoding之外,還有一個影響編碼的參數(shù)useBodyEncodingForURI,關(guān)于這個參數(shù),官方文檔說明如下:
This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding.This
setting is present for compatibility with Tomcat 4.1.x, where the encoding
specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is
false
.Notes:1) This setting is applied only to the query string of a request. Unlike
URIEncoding
it does not affect the path portion of a request URI. 2) If request character encoding is not known (is not provided by a browser and is not set bySetCharacterEncodingFilter
or a similar filter using Request.setCharacterEncoding method), the default encoding is always "ISO-8859-1". TheURIEncoding
setting has no effect on this default.
其作用大致概括下就是,contentType中指定的編碼是否要替換URIEncoding已經(jīng)設(shè)置的值?;蛘哒f將Body的編碼用于URI的參數(shù)解析,這個Body的編碼有兩方面的來源:
request.setCharacterEncoding()
request header中設(shè)置的contentType中包含的編碼
如果從上述兩個地方能夠獲取到charset信息,同時useBodyEncodingForURI參數(shù)也設(shè)置為true,此時GET請求的參數(shù)解析也使用同一個charset。
如果兩個地方都拿不到charset,那Body的編碼會使用默認(rèn)值ISO-8859-1,這個一般都能注意到。但這個參數(shù)設(shè)置為true時,相當(dāng)于有個隱含的條件,即GET請求的編碼也會使用默認(rèn)編碼ISO-8859-1,而無論你之前的URIEncoding設(shè)置的是什么編碼。
那這種情況下,雖然URIEncoding設(shè)置的編碼可以正常處理中文,但再配置上了這個開并,卻并沒有按照其規(guī)定設(shè)置,就會導(dǎo)致亂碼再次產(chǎn)生。而Tomcat8默認(rèn)已經(jīng)將URIEncoding設(shè)置為UTF-8,如果配置中指定useBodyEncodingForURI這一項,亂碼就出現(xiàn)了。
這一配置,默認(rèn)項為false,
/**
* URI encoding as body.
*/
protected boolean useBodyEncodingForURI = false;
使用這一配置是否生效的地方,在這里
// getCharacterEncoding() may have been overridden to search for
// hidden form field containing request encoding
String enc = getCharacterEncoding();
boolean useBodyEncodingForURI = connector.getUseBodyEncodingForURI();
if (enc != null) {
parameters.setEncoding(enc);
if (useBodyEncodingForURI) {
parameters.setQueryStringEncoding(enc);
}
} else {
parameters.setEncoding
(org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING);
if (useBodyEncodingForURI) { //這里的配置是比較容易忽略的地方
parameters.setQueryStringEncoding
(org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); }
}
再看對于enc變量的獲取
coyote包中的Request類:/**
* Get the character encoding used for this request.
*/
public String getCharacterEncoding() {
if (charEncoding != null) { //這里的值即是通過request.setCharacterEncoding設(shè)置的
return charEncoding;
}
charEncoding = getCharsetFromContentType(getContentType());
return charEncoding;
}
關(guān)于從ContentType中獲取charSet的代碼的代碼此處就不再羅列了。
簡要總結(jié)如下:
影響URI參數(shù)解析編碼(GET請求)的地方共有:
1. Connector 的URIEncoding 配置
2. Connector的useBodyEncodingForURI配置,此配置為true時則直接使用ContentType的charset或者request.setCharacterEncoding指定的encoding。
注意,request設(shè)置的會先被使用。
3. 特別注意,當(dāng)設(shè)置useBodyEncodingForURI為true時,如果getCharacterEncoding為空,即request未設(shè)置編碼,并且ContentType也未配置charset,則queryString會被設(shè)置為ISO-8859-1
以上就是關(guān)于“tomcat GET請求傳參亂碼問題怎么解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:tomcatGET請求傳參亂碼問題怎么解決
網(wǎng)頁URL:http://aaarwkj.com/article0/peedio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、手機網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站改版、Google、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)