您好,我创建了一个 Filter 类并配置了 web.xml,如下所示:
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>my.web.auth.LoginFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>This param is for testing</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/html/test/*</url-pattern>
</filter-mapping>
当我访问 URL http://{myJBoss}/html/test/index.htm 时, LoginFilter 的 init() 被调用,但 doFilter() 未被调用
这是我的 Filter 类的摘录:
public void init(FilterConfig config) throws ServletException {
log.debug("[201207bb] init"); //******This line can be seen in log file
this.config = config;
String testParam = config.getInitParameter("test-param");
log.debug("test-param="+testParam); //******* This is output correctly too
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
log.debug("[201207bb] doFilter"); //*****This line didn't appear in log file
HttpServletRequest request = (HttpServletRequest) req;
//Get the IP address of client machine.
String ipAddress = request.getRemoteAddr();
//Log the IP address and current timestamp.
log.debug("IP "+ipAddress + ", Time " + new Date().toString());
chain.doFilter(req, res);
}
有谁知道为什么会这样吗? 我也尝试过jsp,同样的结果。
最佳答案
问题已解决。 url-pattern 设置不正确。 调用 init() 并不意味着 url-pattern 设置正确。
关于java Filter 类 doFilter() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11571867/