Listener:监听器
-
概念:web的三大组件之一。
-
事件监听机制
- 事件 :一件事情
- 事件源 :事件发生的地方
- 监听器 :一个对象
-
注册监听:将事件、事件源、监听器绑定在一起。 当事件源上发生某个事件后,执行监听器代码。
-
ServletContextListener:监听ServletContext对象的创建和销毁
-
方法:
- void contextDestroyed(ServletContextEvent sce) :ServletContext对象被销毁之前会调用该方法。
- void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调用该方法。
-
实现步骤:
- 定义一个类,实现ServletContextListener接口
- 复写方法
- 配置
1.web.xml配置
<listener>
<listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class>
</listener>
* 指定初始化参数<context-param>
2.注解配置
@WebListener