在网上有很多这方面文章,依照上面也解决过,但是修改了mysql的配置文件my.ini.感觉在部署时不是很爽.
这里给出另一种解决方法(mysql-4.1.11).低版本的mysql似乎没出现过中文问题(mysql-4.0.17). :)
1.添加一个过滤器,将字符集编码设为UTF-8.
修改web.xml:
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>hxz.filter.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
新建一个过滤器:
package hxz.filter;
import java.io.IOException;
import javax.servlet.*;
public class SetEncodingFilter implements Filter {
// default character encoding
String defaultEncoding = "UTF-8";
public void init(FilterConfig config) throws ServletException {
String encoding = config.getInitParameter("encoding");
if (encoding != null) {
defaultEncoding = encoding;
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(defaultEncoding);
chain.doFilter(request, response);
}
public void destroy() {
defaultEncoding = null;
}
}
2.修改hibernate配置文件:
<property name="url">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</value>
</property>
注意后面的:useUnicode=true&characterEncoding=UTF-8, 在xml中&要改为;amp;
3.在新版本中mysql建表时,你可以选择一种字符集,将它设为utf-8.
jsp或servlet中把字符集改为UTF-8.
4.trying...
posted on 2007-08-13 15:36
冬天出走的猪 阅读(157)
评论(0) 编辑 收藏 所属分类:
Database