為什麼過濾器只執行一次?

新疆朋友發表於2003-07-11
我編寫了一個測試的過濾器,當我第一次裝載一個servlet時,過濾器發揮作用,重複重新整理該servlet時,過濾器沒有被呼叫,是否是我的配置有問題,如果真是這樣,就無法使用它來做認證了。請高手指點。謝謝
我的web.xml:
<web-app>
<filter>
<filter-name>Servlet Mapped Filter</filter-name>
<filter-class>filters.ExampleFilter</filter-class>
<init-param>
<param-name>attribute</param-name>
<param-value>filters.ExampleFilter.SERVLET_MAPPED</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Servlet Mapped Filter</filter-name>
<servlet-name>invoker</servlet-name>
</filter-mapping>

<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>


過濾器程式:
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public final class ExampleFilter implements Filter {


private String attribute = null;
private FilterConfig filterConfig = null;
public void destroy() {
System.out.println("enter filter.destory()");
this.attribute = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
System.out.println("enter filter.doFilter()");
// Store ourselves as a request attribute (if requested)
if (attribute != null)
request.setAttribute(attribute, this);

// Time and log the subsequent processing
long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
String url = ((HttpServletRequest)request).getRequestURI();
filterConfig.getServletContext().log
(this.toString() + "(" + url + "): " + (stopTime - startTime) +
" milliseconds");
}

public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("enter filter.init()");
this.filterConfig = filterConfig;
this.attribute = filterConfig.getInitParameter("attribute");

}
public String toString() {

if (filterConfig == null)
return ("InvokerFilter()");
StringBuffer sb = new StringBuffer("InvokerFilter(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}
}

相關文章