温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

tomcat GET请求传参乱码问题怎么解决

发布时间:2022-01-14 10:40:39 来源:亿速云 阅读:227 作者:iii 栏目:大数据

这篇“tomcat GET请求传参乱码问题怎么解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“tomcat GET请求传参乱码问题怎么解决”文章吧。

在Tomcat的通道(Connector)配置中,除了URIEncoding之外,还有一个影响编码的参数useBodyEncodingForURI,关于这个参数,官方文档说明如下:


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已经设置的值。或者说将Body的编码用于URI的参数解析,这个Body的编码有两方面的来源:

  • request.setCharacterEncoding()

  • request header中设置的contentType中包含的编码

如果从上述两个地方能够获取到charset信息,同时useBodyEncodingForURI参数也设置为true,此时GET请求的参数解析也使用同一个charset。

如果两个地方都拿不到charset,那Body的编码会使用默认值ISO-8859-1,这个一般都能注意到。但这个参数设置为true时,相当于有个隐含的条件,即GET请求的编码也会使用默认编码ISO-8859-1,而无论你之前的URIEncoding设置的是什么编码。

那这种情况下,虽然URIEncoding设置的编码可以正常处理中文,但再配置上了这个开并,却并没有按照其规定设置,就会导致乱码再次产生。而Tomcat8默认已经将URIEncoding设置为UTF-8,如果配置中指定useBodyEncodingForURI这一项,乱码就出现了。

这一配置,默认项为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设置的
       return charEncoding;
   }
   charEncoding = getCharsetFromContentType(getContentType());
   return charEncoding;
}

关于从ContentType中获取charSet的代码的代码此处就不再罗列了。

简要总结如下:

影响URI参数解析编码(GET请求)的地方共有:
1. Connector 的URIEncoding 配置
2.  Connector的useBodyEncodingForURI 配置,此配置为true时则直接使用ContentType的charset或者request.setCharacterEncoding指定的encoding。
注意,request设置的会先被使用。
3. 特别注意,当设置useBodyEncodingForURI为true时,如果getCharacterEncoding为空,即request未设置编码,并且ContentType也未配置charset,则queryString会被设置为ISO-8859-1

以上就是关于“tomcat GET请求传参乱码问题怎么解决”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI