学海无涯

记录我的程序人生...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
1, mysql安装时设置字符集utf-8, jdbc驱动3.0.15以上.
2. hibernate配置文件中,加上属性
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>

3. web.xml设置Filter
=================================================
<filter>
  
<filter-name>
   Set Web Application Character Encoding
  
</filter-name>
  
<filter-class>cn.com.commnet.util.SetEncodeFilter</filter-class>
  
<init-param>
   
<param-name>defaultencoding</param-name>
   
<param-value>UTF-8</param-value>
  
</init-param>
 
</filter>
 
<filter-mapping>
  
<filter-name>
   Set Web Application Character Encoding
  
</filter-name>
  
<url-pattern>/*</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;
    }


}
posted on 2005-03-16 12:03  josson  阅读(4149)  评论(1编辑  收藏  举报