文章目录
1 Listener
1.1 Listener简介
Listener(监听器)是JavaWeb的三大组件之一
JavaWeb的三大组件
Servlet程序,Filter过滤器,Listener监听器
Listener是JavaEE的一种规范,规范就是接口,Listener的作用:
监听某种事物的变化,然后通过hook,反馈给程序做一些相应的处理
目前最多使用的就是ServletContextListener
1.2 ServletContextListener
1.2.1 ServletContextListener的功能
ServletContextListener可以监听ServletContext对象的创建和销毁
监听到ServletContext对象创建/销毁之后,会分别调用ServletContextListener的创建/销毁hook进行处理
contextInitialized()方法在ServletContext对象创建后被立刻调用
contextDestroyed()方法在ServeltContext对象销毁后被立刻调用
ServletContext对象在Web过程启动的时候创建,在Web工程停止时销毁
1.2.2 ServletContextListener的使用
使用步骤:
1,编写一个类实现ServletContextListener接口
2,重写两个方法
contextInitialized()和contextDestroyed()
3,在web.xml中配置监听器
1,实现ServletContextListener接口并重写所需方法
2,在web.xml中配置监听器
3,监听器效果演示