在网上有很多这方面文章,依照上面也解决过,但是修改了mysql的配置文件my.ini.感觉在部署时不是很爽.
就是default-character-set=gbk ,可以把gbk改成别的。这里给出另一种解决方法(mysql-5.5). :)低版本的mysql似乎没出现过中文问题
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.
//要注意当你从jsp页里得到数据时不要//String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");只要
String username=request.getParameter("username");就可以了!!!!!!!!!!!!
4.trying...
posted on 2007-02-05 17:30
EricWong 阅读(482)
评论(0) 编辑 收藏 所属分类:
Java