1、修改MySql数据库的my.ini配置文件、
# CLIENT SECTION
#
----------------------------------------------------------------------
#
#
The following options will be read by MySQL client applications.
# Note that
only client applications shipped by MySQL are guaranteed
# to read this
section. If you want your own MySQL client program to
# honor these values,
you need to specify it as an option during the
# MySQL client library
initialization.
#
[client]
port=3306
default-character-set=GBK
#此处默认编码修改为GBK或utf-8
# SERVER SECTION
#
----------------------------------------------------------------------
#
2、创建数据表的时候
# 主机: localhost
# 数据库: cits
# 表: 'article'
#
CREATE TABLE
`article` (
`article_id` varchar(100) NOT NULL default '0',
`article_title` varchar(100) default NULL,
`domain_id` varchar(100) default
NULL,
`article_text` varchar(100) default '',
`good_flg` tinyint(1)
default NULL,
`lock_flg` tinyint(1) default NULL,
`top_flg` tinyint(1)
default NULL,
`read_times` int(11) default NULL,
`reply_times` int(11)
default NULL,
`last_reply` char(6) default NULL,
`last_reply_time`
datetime default NULL,
`add_user` varchar(30) default NULL,
`add_date`
datetime default NULL,
`upd_user` varchar(30) default NULL,
`upd_date`
datetime default NULL,
PRIMARY KEY (`article_id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
此处要用GBK编码
3、在jsp页面中使用UTF-8编码
Object init = request.getAttribute("init");
String domainId =
(String)request.getAttribute("domainId");
if ((init == null) ||
(init.equals("")) || (init.equals("null")))
{
response.sendRedirect("./listArticle.do?domainId="+domainId);
} else
{
%>
page import ="java.util.HashMap"%>
="java.util.List"%>
List
articleList = (List)request.getAttribute("articleList");
//String domainId =
(String)request.getAttribute("domainId");
int size = 0;
if(articleList
!=null){
size = articleList.size();
}
%>
/p>
Transitional//EN">
帖子列表href="articleSave.jsp?domainId=">发表新主题
bordercolor="#6666FF">
bgcolor="#6666FF">
align="center">回/点击
bgcolor="#6666FF">
align="center">标题
bgcolor="#6666FF">
align="center">发表时间
bgcolor="#6666FF">
align="center">发帖人
bgcolor="#6666FF">
align="center">最后回复
align="center">
align="center">
align="center">
align="center">
align="center">
4、给Web应用加上过滤器,做一个Fiter,对所有请求统一使用UTF-8编码
下面是Fiter的源代码
package com.util.common;
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 {
protected
String encoding = null;
protected FilterConfig filterConfig =
null;
protected boolean ignore = true;
public void init(FilterConfig filterConfig) throws ServletException
{
// TODO Auto-generated method stub
this.filterConfig =
filterConfig;
this.encoding =
filterConfig.getInitParameter("encoding");
System.out.println("====initEncodingFilter");
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;
}
}
public void doFilter(ServletRequest request, ServletResponse
response,
FilterChain chain) throws IOException, ServletException
{
// TODO Auto-generated method stub
if(ignore||(request.getCharacterEncoding() == null)){
String
encoding = selectEncoding(request);
if(encoding!=null){
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request,response);
}
public void destroy() {
// TODO Auto-generated method
stub
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest
request){
return(this.encoding);
}
}
5、在Web.xml中配置过滤器,使之能起过滤作用
在web.xml中加入下面代码
Encoding
com.util.common.EncodingFilter
encoding
utf-8
Encoding
/*
6、在hibernate.cfg.xml里连接数据的属性项修改成如下代码
name="connection.url">
jdbc:mysql://localhost:3306/cits?useUnicode=true&characterEncoding=UTF-8
本操作步骤在struts1.2 + hibernate 3.1 +mysql 5.0
Windows2000平台上通过,可解决乱码问题,其他的数据库请读者自行测试