欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

tomcatGET請(qǐng)求傳參亂碼問題怎么解決

這篇“tomcat GET請(qǐng)求傳參亂碼問題怎么解決”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“tomcat GET請(qǐng)求傳參亂碼問題怎么解決”文章吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、蓋州網(wǎng)站維護(hù)、網(wǎng)站推廣。

在Tomcat的通道(Connector)配置中,除了URIEncoding之外,還有一個(gè)影響編碼的參數(shù)useBodyEncodingForURI,關(guān)于這個(gè)參數(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". The URIEncoding setting has no effect on this default.

其作用大致概括下就是,contentType中指定的編碼是否要替換URIEncoding已經(jīng)設(shè)置的值。或者說將Body的編碼用于URI的參數(shù)解析,這個(gè)Body的編碼有兩方面的來源:

  • request.setCharacterEncoding()

  • request header中設(shè)置的contentType中包含的編碼

如果從上述兩個(gè)地方能夠獲取到charset信息,同時(shí)useBodyEncodingForURI參數(shù)也設(shè)置為true,此時(shí)GET請(qǐng)求的參數(shù)解析也使用同一個(gè)charset。

如果兩個(gè)地方都拿不到charset,那Body的編碼會(huì)使用默認(rèn)值ISO-8859-1,這個(gè)一般都能注意到。但這個(gè)參數(shù)設(shè)置為true時(shí),相當(dāng)于有個(gè)隱含的條件,即GET請(qǐng)求的編碼也會(huì)使用默認(rèn)編碼ISO-8859-1,而無論你之前的URIEncoding設(shè)置的是什么編碼。

那這種情況下,雖然URIEncoding設(shè)置的編碼可以正常處理中文,但再配置上了這個(gè)開并,卻并沒有按照其規(guī)定設(shè)置,就會(huì)導(dǎo)致亂碼再次產(chǎn)生。而Tomcat8默認(rèn)已經(jīng)將URIEncoding設(shè)置為UTF-8,如果配置中指定useBodyEncodingForURI這一項(xiàng),亂碼就出現(xiàn)了。

這一配置,默認(rèn)項(xiàng)為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); }
}

再看對(duì)于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請(qǐng)求)的地方共有:
1. Connector 的URIEncoding 配置
2.  Connector的useBodyEncodingForURI配置,此配置為true時(shí)則直接使用ContentType的charset或者request.setCharacterEncoding指定的encoding。
注意,request設(shè)置的會(huì)先被使用。
3. 特別注意,當(dāng)設(shè)置useBodyEncodingForURI為true時(shí),如果getCharacterEncoding為空,即request未設(shè)置編碼,并且ContentType也未配置charset,則queryString會(huì)被設(shè)置為ISO-8859-1

以上就是關(guān)于“tomcat GET請(qǐng)求傳參亂碼問題怎么解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

標(biāo)題名稱:tomcatGET請(qǐng)求傳參亂碼問題怎么解決
網(wǎng)頁URL:http://www.chinadenli.net/article0/peedio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司手機(jī)網(wǎng)站建設(shè)ChatGPT網(wǎng)站改版Google小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司