1,配置mysql服务器时,在选择字符编码时选择utf-8(如果以前选择了其它编码,需要将数据结构导出,然后重新导入数据结构)
2,每个jsp页面都加上如下语句(注意,不能将下面两行写在一个jsp文件,然后将那个文件包含在所有的jsp文件中)
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
3,hibernate下配置连接时,加上如下属性
<property name="useUnicode">true</property>
<property name="characterEncoding">utf8</property>
4,加过滤器,代码如下

/**//*
*
*/
package com.tanyaowu.taiji.filter;
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;


/** *//**
*
* @描述
* @作者 谭耀武
* @日期 2005-7-23
* 修改记录: 修改者 修改内容 修改日期
*
*/

public class CharEncodingFilter 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 void destroy()
{
this.encoding = null;
this.filterConfig = null;
}

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.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 String selectEncoding(ServletRequest request)
{
return (this.encoding);
}
}

5,web.xml文件中配置如下
<filter>
<filter-name>Char_Encoding</filter-name>
<filter-class>
com.tanyaowu.taiji.filter.CharEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Char_Encoding</filter-name>

<url-pattern>/**//*</url-pattern>
</filter-mapping>

其实原理就是,所有的编码都要一样,如果同志们喜欢用gbk,那么将前面的utf-8换成gbk就OK了.注意hibernate配置文件是utf8而不是utf-8.
另外一点要提醒的是,我们在向myslq插数据时,要保证中文是utf-8的,我在用ue编码sql然后插入时,mysql会报错,所以建议用eclipse编码sql,因为eclipse可以很方便地指定编码(后记:在ue中可以通过开启"高级"/"配置"/"常规"的"自动检测utf-8文件"来正确显示utf-8编码的文件)
posted on 2007-07-25 13:52
Java_Tan 阅读(549)
评论(0) 编辑 收藏