码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb中遇到的字符编码问题

时间:2019-11-02 11:34:59      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:and   query   coding   play   line   server   using   star   class   

一、常见的编码方式

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中遇到的字符编码问题

标签:and   query   coding   play   line   server   using   star   class   

原文地址:https://www.cnblogs.com/Dreamice/p/11780776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!