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>
参考资料:
1、Tomcat7项目迁移到Tomcat8中文乱码问题
2、Java web中常见编码乱码问题(一)
JavaWeb中遇到的字符编码问题的更多相关文章
- JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码
JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码 准备条件:一个创建好的 JavaWeb 项目 步骤: 1.创建一个类并实现 Filter 接口 import javax.ser ...
- Java web应用中的常见字符编码问题的解决方法
以下是 Java Web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- [转载]Java web应用中的常见字符编码问题的解决方法
以下是 Java web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- Python 2中万恶的字符编码
Python2中如果文件存在中文,必须要指定#-*- coding:utf8 -*-或#coding:utf8,否则会报错.那这是为什么呢? 一.原理解析 我们知道,在计算机发展初期,计算机只能识别字 ...
- Java中常用的字符编码-解析
ASCII字符编码 美国信息互换标准代码,为罗马字母编制的一套编码,主要用于表达现代英语和其他西欧语言中的字符,1字节的7位表示一个字符. ISO-8859-1字符编码 ISO为西欧语言中的字符制定的 ...
- Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode
摘要:Python中文虐我千百遍,我待Python如初恋.本文主要介绍在Python2/3交互模式下,通过对中文.英文的处理输出,理解Python的字符编码与解码问题(以点破面). 前言:字符串的编码 ...
- 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题
当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...
- mysql中设置默认字符编码为utf-8
使用过Linux的同志就知道,在Linux下安装mysql,尤其是使用yum安装的时候,我们是没法选择其默认的字符编码方式.这个就是一个比较头痛的问题,如果Linux数据库中使用到中文的时候,乱码问题 ...
- Unity3d发布的iOS产品中使用GB2312字符编码(CP936)
最近在开发中要用到GB2312字符编码(CP936),在C#代码中便有了如此代码 System.Text.Encoding.GetEncoding() 这在Unity3d 编辑器下运行没有任何问题,打 ...
随机推荐
- SLAM资料
当下SLAM方案的总体介绍 http://wwwbuild.net/roboteasy/908066.html slam基础知识 https://www.zhihu.com/question/3518 ...
- 使用迅为itop4418开发板创建Android模拟器
基于迅为iTOP-4418开发部在 Eclipse 中,单击“Windows”菜单,选择“Android Virtual Device Manager”启动 模拟器管理插件.然后如下图,单击“Crea ...
- 20199324《Linux内核原理与分析》第十一周作业
SET-UID程序漏洞实验 一.实验简介 Set-UID 是 Unix 系统中的一个重要的安全机制.当一个 Set-UID 程序运行的时候,它被假设为具有拥有者的权限.例如,如果程序的拥有者是root ...
- CentOS 7 准备 Superset 环境
安装 anaconda 和 superset 下载 anaconda 软件: https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86 ...
- tensorflow(二)
使用图来表示计算任务 在被称之为session的上下文中执行图 使用tensor表示数据 通过变量来维护状态 使用feed和fetch可以为任意的操作复制或者从其中获取数据 tensorflow是一个 ...
- 形参和实参|默认值|可选实参|tuple|*tuple|args|*args | **kwargs|args[:]|
#!/usr/bin/python def hello(i,greet='long time to see!'): out = "hello "+i+" "+g ...
- 《VSTO开发中级教程》刘永富 著 清华大学出版社 在线购买
现在可以和作者 刘永富 通过“二手书直卖”这个APP直接买书. 二手书直卖 的下载方法: 方法一:加QQ群61840693,群共享中搜索“二手书直卖”,下载后打开即可. 方法二:从本帖下载:二手书直卖 ...
- 吴裕雄--天生自然python学习笔记:python 创建和操作Firebase实时数据库
Fire base 是专为移动开发提供的后端服务平台, Firebase 数据库与传统数据库不同,它不是用数据表来存储数据, 而是用 Key . Value 的字典型结构来存储数据,所以它不仅 是轻量 ...
- iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码
iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...
- iOS UICollectionViewCell 的拖动
1.长按cell的情况下实现拖动,所以理应想到用长按手势. 2.既然实现移动cell,就要看看UICollectionView 有没有方法或者协议可以移动的.通过查看UICollectionView的 ...