你的位置: Kiyo'Space首页 数据库 阅读文章 欢迎留下您的足迹

MySQL乱码的问题

[ 数据库 ] 分享

如果插入前是正常的,那么useUnicode=true;characterEncoding=gbk应该就不会有问题了,估计是插入前就是乱码,如果编个filter,
public class SetCharEncode 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 {
String encoding = selectEncoding(request);
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
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);
}
}
在web.xml配置:
<filter>
<filter-name>setchar</filter-name>
<filter-class>com.xdg.filter.SetCharEncode</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setchar</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


欢迎在此留下您的脚步






评论内容 (*必填):
(Ctrl + Enter 快速提交)