2012-05-16 64 views

回答

0

沒有Filter不適用。無論Filter在做什麼,應該最有可能由ServletContextListener代替。

@WebListener 
public class Config implements ServletContextListener { 

    @Override 
    public void contextInitialized(ServletContextEvent event) { 
     // Do stuff during server startup. 
    } 

    @Override 
    public void contextDestroyed(ServletContextEvent event) { 
     // Do stuff during server shutdown. 
    } 

} 

當你還沒有上的Servlet 3.0然而,除去@WebListener和如下注冊它在web.xml老fasioned方式:

<listener> 
    <listener-class>com.example.Config</listener-class> 
</listener> 

如果你真的堅持在使用Filter,你可以在工作中濫用init()方法,並在doFilter()方法中調用chain.doFilter(request, response)

0

關於方式的一個回合將是類變量,並將其設置爲第一次訪問,並在檢查此變量的'if'塊中具有所有功能代碼。

相關問題