我思故我强

使用filter解决中文乱码问题


使用filter解决中文乱码问题

关键字:   使用filter解决中文乱码问题    

一.在web.xml中配置

xml 代码
  1. < filter >   
  2.          < filter-name > EncodingFilter </ filter-name >   
  3.          < filter-class > com.kenshin.base.SysFilter </ filter-class >   
  4.          < init-param >   
  5.              < param-name > encoding </ param-name >   
  6.              < param-value > UTF-8 </ param-value >   
  7.          </ init-param >   
  8.          < init-param >   
  9.              < param-name > enable </ param-name >   
  10.              < param-value > true </ param-value >   
  11.          </ init-param >   
  12.      </ filter >   
  13.      < filter-mapping >   
  14.          < filter-name > EncodingFilter </ filter-name >   
  15.          < url-pattern > *.do </ url-pattern >   
  16.      </ filter-mapping >   
 

二.filter类代码

java 代码
  1. package  com.kenshin.base;   
  2.   
  3. import  java.io.IOException;   
  4.   
  5. import  javax.servlet.Filter;   
  6. import  javax.servlet.FilterChain;   
  7. import  javax.servlet.FilterConfig;   
  8. import  javax.servlet.ServletException;   
  9. import  javax.servlet.ServletRequest;   
  10. import  javax.servlet.ServletResponse;   
  11.   
  12. import  org.apache.commons.logging.Log;   
  13. import  org.apache.commons.logging.LogFactory;   
  14. import  org.hibernate.Session;   
  15. import  org.hibernate.SessionFactory;   
  16. import  org.hibernate.Transaction;   
  17.   
  18. public   class  SysFilter  implements  Filter {   
  19.      protected  String sEncodingName;   
  20.   
  21.      protected  FilterConfig sysFilter;   
  22.   
  23.      protected   boolean  bEnable;   
  24.   
  25.      protected  Log logger = LogFactory.getLog(SysFilter. class );   
  26.   
  27.      public   void  destroy() {   
  28.   
  29.     }   
  30.   
  31.      public   void  doFilter(ServletRequest arg0, ServletResponse arg1,   
  32.             FilterChain arg2)  throws  IOException, ServletException {   
  33.          // TODO Auto-generated method stub   
  34.          if  ( this .bEnable) {   
  35.              try  {      
  36.                 arg0.setCharacterEncoding( this .sEncodingName);   
  37.                 arg1.setContentType( "text/html;charset="  +  this .sEncodingName);   
  38.                 arg1.setCharacterEncoding( this .sEncodingName);   
  39.                 arg2.doFilter(arg0, arg1);   
  40.   
  41.             }  catch  (Exception e) {   
  42.                 logger.info( "出错了" );   
  43.             }   
  44.              // session.close();   
  45.         }  else  {   
  46.             arg2.doFilter(arg0, arg1);   
  47.         }   
  48.     }   
  49.   
  50.      public   void  init(FilterConfig arg0)  throws  ServletException {   
  51.          // TODO Auto-generated method stub   
  52.          this .sysFilter = arg0;   
  53.          this .loadFilterSetting();   
  54.     }   
  55.   
  56.      private   void  loadFilterSetting() {   
  57.          this .sEncodingName =  this .sysFilter.getInitParameter( "encoding" );   
  58.         logger.info( "encoding:"  + sEncodingName);   
  59.         String sEnable =  this .sysFilter.getInitParameter( "enable" );   
  60.          if  (sEnable !=  null  && sEnable.equalsIgnoreCase( "true" )) {   
  61.              this .bEnable =  true ;   
  62.         }  else  {   
  63.              this .bEnable =  false ;   
  64.         }   
  65.     }   
  66.   
  67.   
  68. }   

posted on 2007-09-20 20:13 李云泽 阅读(361) 评论(0)  编辑  收藏 所属分类: Tomcat


只有注册用户登录后才能发表评论。


网站导航: