1. mysql安装时设置字符集utf-8, jdbc驱动3.0.15以上.
2. hibernate配置文件中,加上属性
true
UTF-8
3. web.xml设置Filter
=================================================
Set Web Application Character Encoding
filter-name>
cn.com.commnet.util.SetEncodeFilterfilter-class>
defaultencodingparam-name>
UTF-8param-value>
init-param>
filter>
Set Web Application Character Encoding
filter-name>
/*url-pattern>
filter-mapping>
=============================================
SetEncodeFilter.java
public class SetEncodeFilter implements Filter {
protected FilterConfig filterConfig = null;
protected String defaultEncoding = null;
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = arg0;
this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(selectEncoding(request));
chain.doFilter(request, response);
}
public void destroy() {
this.defaultEncoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return this.defaultEncoding;
}
}
分享到:
2009-07-15 09:01
浏览 695
评论