JavaWeb中遇到的字符编码问题

Posted dreamice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb中遇到的字符编码问题相关的知识,希望对你有一定的参考价值。

一、常见的编码方式

1、UTF-8

2、ISO-8859-1

二、Tomcat的编码问题

Tomcat8和7的编码方式

Tomcat7对URI默认编码是ISO-8859-1
Tomcat8对URI默认编码是UTF-8

官网解释:
https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
URIEncoding :This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.
https://tomcat.apache.org/tomcat-8.0-doc/config/http.html
URIEncoding :This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

在Tomcat包内也可以查找到:webapps→docs→config→http.html

URIEncoding 和useBodyEncodingForURI

以Tomcat8为例:

(1)、URIEncoding
This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

(2)、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 by SetCharacterEncodingFilter 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.

可以理解为:
设置URIEncoding参数可以是对所有GET方式的请求的数据进行统一的重新编码,默认编码为 UTF-8;
设置useBodyEncodingForURI为true时,请求查询参数的编码和contentType的设置一致

三、解决方案

1、URIEncoding

<Connector connectionTimeout="20000" port="9080" protocol="HTTP/1.1"  
redirectPort="443" URIEncoding="UTF-8" />

URIEncoding只对URL中的参数进行编码

2、useBodyEncodingForURI="true"

设置useBodyEncodingForURI=true时,就会将请求参数和请求体中的参数根据request.setCharacterEncoding或者contentType中的字符集编码。

3、request.setCharacterEncoding("UTF-8")

servlet等Java后台程序中使用request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");这个方法对于请求Body中的字符编码才有作用,也就是基本只对POST产生作用,终究的原因是GET是HTTP服务器处理,而POST是WEB容器处理

4、encodingFilter

<filter>
        <filter-name>encodingFilter</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>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

5、tomcat7-maven-plugin插件

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <port>9080</port>
        <server>tomcat7</server>
        <uriEncoding>UTF-8</uriEncoding>    
    </configuration>
</plugin>











以上是关于JavaWeb中遇到的字符编码问题的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb的编码问题深入分析

《深入分析JavaWeb技术内幕》读书笔记——中文编码

在python 3.6中处理自定义编码时遇到类型错误

Java web应用中的常见字符编码问题的解决方法

编码转换过滤器

深入分析JavaWeb的中文编码问题