SetCharacterEncodingFilter.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:2k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

Java

  1. /*
  2.  * Created on 2005-6-17
  3.  * Made In GamVan 
  4.  * 
  5.  */
  6. package com.gamvan.filters;
  7. import java.io.IOException;
  8. import javax.servlet.Filter;
  9. import javax.servlet.FilterChain;
  10. import javax.servlet.FilterConfig;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.ServletRequest;
  13. import javax.servlet.ServletResponse;
  14. //import javax.servlet.UnavailableException;
  15. public class SetCharacterEncodingFilter implements Filter {
  16.     protected String encoding = null;
  17.     protected FilterConfig filterConfig = null;
  18.     protected boolean ignore = true;
  19.   
  20.     public void init(FilterConfig filterConfig) throws ServletException {
  21.         this.filterConfig = filterConfig;
  22.         this.encoding = filterConfig.getInitParameter("encoding");
  23.         String value = filterConfig.getInitParameter("ignoer");
  24.         if(value==null)
  25.             this.ignore=true;
  26.         else if(value.equalsIgnoreCase("true"))
  27.             this.ignore=true;
  28.         else if(value.equalsIgnoreCase("yes"))
  29.             this.ignore=true;
  30.         else
  31.             this.ignore = false;
  32.        
  33.     }
  34.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
  35.      throws IOException, ServletException {
  36.         if(this.ignore||(request.getCharacterEncoding()==null)){
  37.             String encoding = selectEncoding(request);
  38.             if(encoding!=null)
  39.                 request.setCharacterEncoding(encoding);
  40.         }
  41.         chain.doFilter(request,response);
  42.     }
  43.     public void destroy() {
  44.         this.encoding = null;
  45.         this.filterConfig = null;
  46.  
  47.     }
  48.     protected String selectEncoding(ServletRequest request) {
  49.         return this.encoding;
  50.     }
  51. }