robbin 写道
MySQL数据库的4.1是一个分水岭,4.1直接支持Unicode,以下版本支持的不好;
MySQL JDBC Driver的3.0.16也是一个分水岭,3.0.16版本会取数据库本身的编码,然后按照该编码转换,这种方式和Oracle的JDBC Driver是一样的。例如你的数据库是GBK编码的话,JDBC Driver就会把数据库里面的取出来的字符串按照GBK往unicode转换,送给JVM。因此正确的设置数据库本身的编码就尤为重要。
MySQL JDBC Driver3.0.16以下的版本则不然,它不会那么智能的根据数据库编码来确定如何转换,它总是默认使用ISO8859-1,因此你必须使用 characterEncoding=GBK来强制他把数据库中取出来的字符串按照GBK来往unicode转换。
因此,使用什么数据库版本,不管是3.x,还是4.0.x还是4.1.x,其实对我们来说不重要,重要的有二:
1) 正确的设定数据库编码,MySQL4.0以下版本的字符集总是默认ISO8859-1,MySQL4.1在安装的时候会让你选择。如果你准备使用UTF- 8,那么在创建数据库的时候就要指定好UTF-8(创建好以后也可以改,4.1以上版本还可以单独指定表的字符集)
2) 使用3.0.16以上版本的JDBC Driver,那么你就不需要再写什么characterEncoding=UTF-8
根据robbin的提示,项目中关于字符集问题的基本解决方案
所用环境是struts2+ spring 2.5+hibernate 3.2 . DB用mysql 5.0
- 数据库的字符集
mysql4.1 之后由于可以设定表的字符集,于是需要注意的就是在创建表的时候设置字符集。
创建表的时候,我们添加如下代码
character set =utf8;
- hibernate读写字符集
因为这里用spring+hibernate,所以在设置dataSource bean的时候进行设计就可以了
<property name="url" value="jdbc:mysql://localhost:3306/100see?useUnicode=true&characterEncoding=utf8" />
- 添加web.xml过滤器
过滤器定义 写道
Servlet过滤器是用户请求和处理程序之间的一层处理程序,能够对与过滤器关联的URL请求和响应进行检查和修改。Servlet过滤器能够在 Servlet被调用之前检查Request对象,修改Request Header和Request内容;在Servlet被调用之后检查Response对象,修改Response Header和Response内容。Servlet过滤器过滤的URL资源可以是Servlet、JSP、HTML文件,或者是整个路径下的任何资源。多个过滤器可以构成一个过滤器链,当请求与过滤器关联的URL的时候,过滤器链上的过滤器就会挨个发生作用,除非某个过滤器终止了这个过程。
由于不同项目的字符集设置有可能导致字符集混乱,因此我们通过设置过滤器,完成servlet之间的字符集转换问题
很多时候我们都会用到spring的过滤器
<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>
但是我们发现spring的过滤器只对request做字符集的过滤。因此建议实现自己的过滤器
package com.starweb.core.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* intERPret request parameters for this request.
*
* @param request
* The Servlet request we are processing
* @param result
* The servlet response we are creating
* @param chain
* The filter chain we are processing
*
* @exception IOException
* if an input/output error occurs
* @exception ServletException
* if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding+"\"");
}
// System.out.println("requestEncoding : " + request.getCharacterEncoding());
// System.out.println("responseEncoding : " + response.getCharacterEncoding());
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request
* The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
- jsp字符集问题
jsp页面设置UTF-8
<%@ page language="java" pageEncoding="UTF-8"%>
- struts2字符集设置
struts2 可以设置字符集。默认情况下iso-8859-1字符集, 设置struts2.xml
<constant name="struts.i18n.encoding" value="UTF-8" />
ps: struts2的国际化有时候失败,自己用filter控制字符集问题比较好
分享到:
相关推荐
Oracle字符集的问题.总结了一些项目中的实际问题,和解决办法
基于python实现的BP神经网络手写数字识别模型实验源码+详细注释+数据集+项目说明+实验结果及总结.7z 人工智能 课程作业 手写数字数据集 BP网络模型识别手写数字 反向传播(英语:Backpropagation,缩写为BP)是...
字符集:UTF-8 开发工具:Eclipse 包括内容: basic 基本语法 oop 面向对象 api 常用api及工具类 javadoc javaDoc的使用 debug 代码调试方法 datastructure 基本数据结构 designpattern 常见设计模式 sorts 基本...
文章目录1、连接数据库时提示密码出错2、mysql的版本与导入的数据所用的版本不同3、navicat中创建数据库时,所定义的字符集与原数据库使用的字符集不同4、mysql配置文件中设置的最大容量不够了5、在phpstudy中重启...
本文是根据项目需要,总结一些网文所写,都是经过测试。内容包括:环境搭建、配置文件结构介绍、写入文件、写入数据库、按大小写入文件、按应用写入文件、按日期生成文件等。
locale 是操作系统中用于描述语言环境的设置,包括字符集、数字格式、日期时间格式、货币格式等。locale 设置影响着操作系统中字符的显示和处理。 Docker 容器的 locale 设置 在 Docker 容器中,默认的 locale 设置...
搭建完Hadoop后,在项目文件`\Data\NBCorpus\Country`中选择几个分类,分成训练集、测试集。 比如该目录下的`CANA`中共有264个文件,分成两个文件夹,随机取177个文件作为训练集、87个作为测试集。两个文件夹的名字...
总结.mp4 ├─11.认识for循环(下).mp4 ├─12.认识字符串 .mp4 ├─13.认识下标和切片(上).mp4 ├─14.认识下标和切片(下).mp4 ├─15字符串常见操作(一) find()方法和index()方法.mp4 ├─16.字符串常见操作...
就算生不逢时,也该理解理解了。对于一些性能调优的话题我是一直插不上嘴,只是针对昨晚看到...这种配置我们一般是不会设置的,因为关于乱码的转换我们会在具体项目中具体处理,直接修改Tomcat的字符集未免过于太死板。
习题集内容覆盖面广,包括:Java言的基本常识、基本语法、面向对象的基本概念、数组、字符串、异常处理、文件和数据流、图形用户界面设计、小应用程序、线程、编程规范、网络程序设计、多媒体民图形学程序设计以及...
AIstudio手写数字识别项目的实验报告,报告中有代码链接。文档包括: 1.数据预处理 2.数据加载 3.网络结构尝试:简单的多层感知器、卷积神经网络LeNet-5、循环神经网络RNN、Vgg16 4.损失函数:平方损失函数、交叉...
1. 字符串:Python 中用引号引起来的字符集称之为字符串。字符串可以使用单引号、双引号或三引号。转义字符可以转义很多字符,如\n 表示换行、\t 表示制表符、\\ 表示字符\。 2. 整数与浮点数:Python 可以处理任意...
45_深入探索Java字符集编解码 46_字符集编解码全方位解析 47_Netty服务器与客户端编码模式回顾及源码分析准备 48_Netty与NIO系统总结及NIO与Netty之间的关联关系分析 49_零拷贝深入剖析及用户空间与内核空间切换方式...
39_NIO中Scattering与Gathering深度解析 40_Selector源码深入分析 41_NIO网络访问模式分析 42_NIO网络编程实例剖析 43_NIO网络编程深度解析 44_NIO网络客户端编写详解 45_深入探索Java字符集编解码 46_字符集编解码...
4. MNIST 数据集:MNIST 是一个非常有名的手写体数字识别数据集,被广泛用作机器学习的入门样例,包含了 60000 张图片作为训练数据,10000 张图片作为测试数据,每一张图片代表了 0~9 中的一个数字。 5. Matlab:...
C.1 字符串、字符集和转换 C.2 与MFC共享的类 C.3 实现COM服务器 C.4 ActiveX控件和控件容纳 C.5 ATL_MIN_CRT变化 C.6 总结 附录D 属性化ATL D.1 ATL Attributes的基础 D.2 属性化ATL的未来 D.3 总结 索引
第45讲:深入探索Java字符集编解码 第46讲:字符集编解码全方位解析 第47讲:Netty服务器与客户端编码模式回顾及源码分析准备 第48讲:Netty与NIO系统总结及NIO与Netty之间的关联关系分析 第49讲:零拷贝深入...
2.1.2 创建无项目文件的网站 2.1.3 设计网页 2.2 Visual StudioIDE 2.2.1 解决方案资源管理器 2.2.2 文档窗口 2.2.3 工具箱 2.2.4 错误列表和任务列表 2.2.5 服务器资源管理器 2.3 代码编辑器 ...
数据预测前一系列处理,先进行特征工程处理,结合皮尔逊相关系数,把无用特征进行剔除,完善字符编码格式。再处理类别不平衡的问题(正负样本数相差较多,易导致数据倾斜或不准确)。 四、模型使用与评估 使用机器...