惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 李韦利

解读C#中的规则表达式 Oracle函数大全 作一个真正合格程序员的七种素质 面试官最爱提的问题TOP10 一名董事长给大学生的18条忠告 程序人生:做技术,切不可沉湎于技术 开始上班了 连接MySQl的JavaBean - 李韦利 - 博客园 Jakarta-Tomcat简明中文版用户指南 SQL 语法参考 MD5加密算法简介 jsp实现cookie的使用 JSP生成验证码源程序 Java语言入门级的十二大特色详细介绍 JAVABEAN实现多文件上传的两种方法 J2EE架构学习者的6个最佳实践 html与jsp开发分离技术 安装程序制作简明教程 Unix/Linux命令详解
Mysql与JSP网页中文乱码问题的解决方案
李韦利 · 2006-03-07 · via 博客园 - 李韦利
 

MysqlJSP网页中文乱码问题的解决方案

自从以前学习JSP开始,中文乱码问题就一直不断,苦不堪言。这次在项目开始之前,我们要解决的第一个问题就是把mysql的中文乱码问题搞定。经过多天的努力,终于成功的解决了中文乱码问题,特写在这里,以备后用。软件及环境:Windows XP(2000), j2sdk1.4.2, Tomcat 5.0.25, mysql 4.1,  EMS Mysql Manager 2(方便建表,版本2.8.5.1),驱动为mysql-connector-java-3.1.4-beta-bin.jar目标:在该环境下,实现中文的正常显示,读取与插入数据库。

注:我只在此环境下测试通过,别的系统及不同版本未测试要点:统一字符集(JSP页面编码,mysql建库时字符集选择,连接数据库URLrequest设定等)下面我以GBK为例讲解。如果要使用utf-8,只要在相应的GBK处换成utf-8即可

---------------------------   

步骤1   GBK字符集建库建表   -------------------------------------我使用EMS来建mysql的数据库及表,因为它是图形界面,方便操作(就像SQL SERVER 2000中的企业管理器一样)。建库时,从EMS菜单中选create Database...新建一个数据库,CharacterSetgbk_bin(另一个gbk_chinese_ci不知道与这个有什么区别,我找资料也没有找到。如果你知道,请告诉我,我补充在这里)。不要把工具栏上有一个加号和数据库模样的图标当成新建数据库了,那个新注册一个已经存在的数据库。
后面建表时,也要选择同样的字符集。建好后,此时不要用EMS向里面插入数据,否则你看到的中文依然是乱码。

---------------------------   

步骤2   连接数据库的URL后加些参数  -------------------------------假设我新建的数据库是testdb,那么我连接数据库的url应该为:

jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=gbk

此时要注意:如果我是把这个url写在JAVA代码中,就直接这样写。但如果是在xml配置文件中(如struts-config.xml,web.xml等),要把其中的&改为&才行,否则会出错。也就是:

jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=gbk

---------------------------   

步骤3   每个JSP页面都要声明该中文字符集  ----------------------------在每个JSP页面的最上面都加上一句

<%@ page language=java contentType=text/html;charset=GBK %>

这样才能保证JSP页面中的中文显示正常

---------------------------   

步骤4   加一个传递参数时设定request字符集的filter  -----------------------因为网络中字符在传递的时候,都是统一以iso-8859-1的编码传递,所以我们必须对request重新设定字符集,才能正常显示中文。如果采用filter类来实现,我们不用在每次取中文参数时都要重新设定。

filter

类的内容:

/*
 * ====================================================================
 *
 *              JavaWebStudio

开源项目
 *              
 *               Struts_db v0.1
 *
 * ====================================================================
 */
package com.strutsLogin.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 SetCharacterEncodingFilter implements Filter {

// ----------------------------------------------------- Instance Variables

/**
* 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);
}

// 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);

}

}//EOC


该代码来自于www.javawebstudio.com,特此感谢!然后我们在web.xml中加一些配置,就可以了,配置如下:

<filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>javawebstudio.struts_db.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
    </init-param>
    <init-param>
        <param-name>ignore</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
</filter-mapping>
放在web.xml的合适位置。一般在最后,<jsp-config>标签之前(如果有的话)经过以上步骤,JSPmysql的中文显示及插入就都正常了。在STRUTS中也正常。但是,此时如果你用EMSmysql的命令行控制台来看表中的数据,却发现它们都是????。这是怎么回事呢?不用担心,只要我们运行下面的这几行命令,就能看到正常的中文了!

SET character_set_client = gbk;
SET character_set_connection = gbk;
SET character_set_database = gbk;
SET character_set_results = gbk;
SET character_set_server = gbk;

SET collation_connection = gbk_bin;
SET collation_database = gbk_bin;
SET collation_server = gbk_bin;
如果你用的是mysql的命令行,则直接输入就好。如果是EMS,则在工具栏中有一个Show SQL Editor按钮,点一下,把上面的命令输入,再按一个execute的按钮,就行了!而且在这种情况下,你可以甚至可以用中文名来建数据库,表名和字段名!!!!

----------------------------------------------------------------------------------------------------

但是有一点要特别注意!GBKUTF-8这样的名字,在mysqlJAVA中有不同的规定,写的时候要格外注意,否则会出错。比如GBK,在JAVA中要写成GBK,但在mysql中要写成gbk(连接数据库的URL)比如UTF-8,在JAVA中要写成UTF-8,但在Mysql中要写成utf8其它的字集符也有类似的区别

</T