Edzy_Java

  BlogJava :: 首页 ::  ::  ::  :: 管理 ::
  58 随笔 :: 12 文章 :: 11 评论 :: 0 Trackbacks
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;

public class EncodingFilter implements Filter {
    
     protected FilterConfig filterConfig;
     private String targetEncoding = "gb2312";
        
        public void init(FilterConfig config) throws ServletException {
            this.filterConfig = config;
            this.targetEncoding = config.getInitParameter("encoding");
        }
        
         public  void doFilter(ServletRequest srequest, ServletResponse  sresponse, FilterChain chain)
            throws IOException, ServletException {
            //System.out.println("使用以下方法对请求进行编码:encoding="+targetEncoding);
                
            HttpServletRequest request = (HttpServletRequest)srequest;
            request.setCharacterEncoding(targetEncoding);
            chain.doFilter(srequest,sresponse);  
        }   

        public void setFilterConfig(final FilterConfig filterConfig)
        {
            this.filterConfig=filterConfig;
        }

        public void destroy()
        {
            this.filterConfig=null;
        }
}

在WEB-INF/web.xml下面配置
   <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.david.common.sys.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>gb2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
posted on 2006-11-15 17:10 lbfeng 阅读(369) 评论(0)  编辑  收藏 所属分类: JSP&Servlet技术

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


网站导航: