Listener
接口;EventListener
JavaWeb三大组件之一;
监听事物的变化,通过回调函数反馈;
ServletContextListener
有八大监听器,这个接口常用;
监听ServletContext的创建和销毁,而servletContext随web工程的生成和销毁而创建和销毁是;
- contextInitialized(ServletContextEvent var1)
- contextDestroyed(ServletContextEvent var1)
接口:
public interface ServletContextListener extends EventListener {
void contextInitialized(ServletContextEvent var1);
void contextDestroyed(ServletContextEvent var1);
}
用例 :
public class ServletListenerTest implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext对象创建");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext对象销毁");
}
}
配置xml:
<!-- Listener-->
<listener>
<listener-class>com.checker.listener.ServletListenerTest</listener-class>
</listener>