2012-07-20 96 views
1

您好,我創建了一個過濾器類,並配置如下web.xml中:Java過濾器類的doFilter()沒有被調用

<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,結果也一樣。

+0

你的意思是doFilter方法在服務器初始化時不被調用?什麼是你的jsp網址? – Jason 2012-07-20 02:56:46

+0

是否有例外? – Raman 2012-07-20 03:20:30

+0

夥計們,感謝您的回覆。 我只是意識到我已經設置了錯誤的url模式。 因爲我認爲當init()被調用意味着URL模式被擊中。但似乎並非如此。 – Bowie 2012-07-20 03:24:14

回答

0

問題解決了。 url-pattern設置不正確。 正在調用init()並不意味着url-pattern設置正確。

+0

什麼url模式修復了這個? – TheCatWhisperer 2016-05-11 18:35:45

相關問題