java过滤器类doFilter()未被调用

时间:2012-07-20 02:02:50

标签: java servlets servlet-filters web.xml

您好,我已经创建了一个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>

当我访问网址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,结果相同。

1 个答案:

答案 0 :(得分:0)

问题解决了。 url-pattern设置不正确。 调用init()并不意味着url-pattern设置正确。