package servletproject;
import java.io.IOException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.PrintWriter;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2011</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */public class ServletFilterTest implements Filter {
    protected FilterConfig filterConfig;
    private String targetEncoding;    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
        this.targetEncoding = filterConfig.getInitParameter("encoding");        logout("FilterConfig 已被创建");
    }
    public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain)
            throws IOException, ServletException {
        logout("使用以下方法对请求进行编码: encoding = " + targetEncoding);        HttpServletRequest request = (HttpServletRequest)srequest;
        //request.setCharacterEncoding(targetEncoding);
        srequest.setCharacterEncoding(targetEncoding);        chain.doFilter(srequest, sresponse);
    }    public void destroy() {
        this.filterConfig = null;
        this.targetEncoding = "";        logout("FilterConfig 已被销毁");
    }
    public void setFilterConfig(final FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }    public void logout(String message) {
        java.io.PrintWriter out = null;        try {
            out = new java.io.PrintWriter(new java.io.FileOutputStream("c:\\test.txt", true));
            out.println(message);
            out.close();
        } catch(Exception e) {
            out.close();
            e.printStackTrace();
        }
    }
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>WebModule1</display-name>
  <filter>
    <filter-name>servletfiltertest</filter-name>
    <filter-class>servletproject.ServletFilterTest</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>gb2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>servletfiltertest</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>为什么在C:\test.txt中显示FilterConfig 已被创建
使用以下方法对请求进行编码: encoding = gb2312
使用以下方法对请求进行编码: encoding = gb2312
FilterConfig 已被销毁“使用以下方法对请求进行编码: encoding = gb2312”显示两次?不是只打印出一次的吗?

解决方案 »

  1.   

    在幾個方法中多打幾個斷點看下,在doFilter中有調用chain.doFilter(srequest, sresponse);Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.就是filter的一個chain調用,總共調用了兩次
      

  2.   


    这个也没有关系吧?我尝试过去掉chain.doFilter(request, response)的,同样是执行两次喔
      

  3.   

    请求的时候和响应的时候URL如果都满足Filter条件,那么就都会执行的。一来一回。。可以用下面的语句看看请求。System.out.println(request.getRequestedURI());
      

  4.   

    详情请看我写的文章:
    http://blog.csdn.net/chaijunkun/article/details/7646338
    另外感谢“xiaobluesky”大神的提示。