监听器-Listener
- 监听器(Listener) 是J2EE Servelet模块下的组件
- Listener的作用对Web应用对象的行为进行监控
- 通过Listener监听自动触发指定的功能代码
三种监听对象
- ServletContext -对全局ServletContext及其属性进行监听
- HttpSession -对用户会话及其属性操作进行监听
- ServletRequest -对请求及属性操作进行监听
开发监听器三要素
- 实现XxxListener接口,不同接口对应不同监听对象
- 实现每个接口中独有的方法,实现触发监听的后续操作
- 在web.xml中配置<listener>使监听器生效
六种常用监听接口
内置对象监听接口
- ServletContextListener - 监听ServletContext对象创建、销毁等操作
- HttpSessionListener - 监听HttpSession对象创建、销毁等操作
- ServletRequestListener - 监听HttpServletRequest对象创建、销毁等
属性监听接口
- ServletContextAttributeListener - 监听全局属性操作
- HttpSessionAttributeListener - 监听用户会话属性操作
- ServletRequestAttributeListener - 监听请求属性操作
web.xml配置
<listener>
<listener-class>ListenerPath</listener-class>
</listener>