Listener监听器
定义:
1、Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监 听器。
2、Listener 它是 JavaEE 的规范,就是接口
3、监听器的作用是,监听某种事物的变化。然后通过回调函数,反馈给客户(程序)去做一些相应的处理
ServletContextListener 监听器
ServletContextListener 它可以监听 ServletContext 对象的创建和销毁。
ServletContext 对象在 web 工程启动的时候创建,在 web 工程停止的时候销毁。
监听到创建和销毁之后都会分别调用 ServletContextListener 监听器的方法反馈。
public interface ServletContextListener extends EventListener{
/***在 ServletContext对 象 创 建 之 后 马 上 调 用 , 做 初 始 化*/
public void contextInitialized(ServletContextEventsce);
/**在 ServletContext对 象 销 毁 之 后 调 用*/
public void contextDestroyed(ServletContextEventsce);
}
监听器实现类:
public class MyServletContextListenerImpl implements ServletContextListener {
@Override public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext 对象被创建了");
}
@Override public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext 对象被销毁了");
}
}
web.xml中配置
<!-配 置 监 听 器 -->
<listener>
<listener-class>com.atguigu.listener.MyServletContextListenerImpl</listener-class>
</listener>

浙公网安备 33010602011771号