JavaWeb——Listener监听器
一、Listener监听器
1. 基本概念
- Listener监听器它是JavaWeb的三大组件之一。JavaWeb的三大组件分别是:Servlet程序、Filter过滤器、Listener监听器。
 - Listener它是JavaEE的规范,就是接口。
 - 监听器的作用是,监听某种事物的变化。然后通过回调函数,反馈给客户(程序)做一些相应的处理。
 
2. 代码实现
- ServletContextListener它可以监听ServletContext对象的创建和销毁。ServletContext对象在web工程启动的时候创建,在web工程停止的时候销毁。监听到创建和销毁之后都会分别调用ServletContextListener监听器的方法反馈。两种方法如下。
 
如何使用ServletContextListener监听器监听ServletContext对象。使用步骤如下:
- 编写一个类去实现ServletContextListener。
 - 实现其两个回调方法。
 - 到web.xml中去配置监听器。
 
监听器实现类如下。
- web.xml中的配置如下。
 
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.




