困扰已久的数据库中文乱码问题终于解决
时间:2007-08-10 来源:linuxchao
在基于struts+hibernat+eclipse(插件Myeclipse)+Mysql的web project的开发中,数据库中文乱码问题一直困扰着我,也一直没有解决,而在今天,这个让我头疼的问题终于解决了,拨开云雾见青天!!!今天把它写下来,希望能给同样遇到此类问题的同志们带来佳音。
我的数据库使用的字符集是utf8,但是不能只在mysql配置文件my.ini中将默认的character-set改为utf8,还要在mysql administrator或其它软件中新建一个Schemata,然后在其中创建表,在创建表的时候要特别注意:在columns and indices中创建完表后,要在Table Options中设置一下Storage Engine和Character Set——
Table Engine设置为MyISAM;
Charset设置为utf8;
然后,如果你的工程以前已经建立过hibernate engineering,即hibernate映射,那么你要重新建立映射了,这样用你的测试类(如果你创建了一个测试类)测试一下保存到数据库中的中文是否是以正常的中文形式显示。如果不出任何异常的话,应该会成功的。
这样只是让后台设置能够运行正常,仍然不能保证在web层传进来的中文数据正常显示。要做到这一点,我是这样做的:
在web层的jsp页面中,pageEncoding和charset我都是设置为utf8,而且都加上了<%setCharacherEncoding("utf8");%>,或许这不必要。然后就要建立一个过滤器了,我的过滤器是这样的:
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 CharsetEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; 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 { if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
//request.setCharacterEncoding("UTF-8");
}
//response.setCharacterEncoding("utf8");
// Pass control on to the next filter
chain.doFilter(request, response); }
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); }
} 创建玩filter,就要在web.xml中配置filter了: <filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>org.hibernate.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
至此,所有事项都应该完成了,如果没有其他异常的话,就能够在web层正常运行web project了。
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 CharsetEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; 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 { if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
//request.setCharacterEncoding("UTF-8");
}
//response.setCharacterEncoding("utf8");
// Pass control on to the next filter
chain.doFilter(request, response); }
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); }
} 创建玩filter,就要在web.xml中配置filter了: <filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>org.hibernate.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
至此,所有事项都应该完成了,如果没有其他异常的话,就能够在web层正常运行web project了。
相关阅读 更多 +