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>
版权声明:
作者:Kiyo
链接:https://www.wkiyo.cn/html/2008-01/i338.html
来源:Kiyo's space
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论