监听器:专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。
Servlet监听器:它用于监听web应用程序中ServletContext、HttpSession、ServletRequest等域对象的创建和销毁事件,以及监听这些域对象中的属性发生修改的事件。
按监听的事件类型Servlet监听器可分为如下三种类型:
—监听域对象自身的创建和销毁的事件监听器
—监听域对象中属性的增加和删除的事件监听器
—监听绑定到HttpSession域中的某个对象的状态的事件监听器
* 域对象创建和销毁的事件监听器就是用来监听ServletContext,HttpSession,HttpServletRequest这三个对象的创建和销毁事件的监听器。
* 域对象的创建和销毁时机
创建时机销毁时机ServletContextweb服务器启动时为每个web应用程序创建相应的ServletContext对象web服务器关闭时为每个web应用程序销毁相应的ServletContext对象HttpSession浏览器开始与服务器会话时创建调用HttpSession.invalidate(),超过了session的最大有效时间间隔,服务器进程被停止ServletRequest每次请求开始时创建每次访问结束后销毁
* ServletContextListener接口用于监听ServletContext对象的创建和销毁事件。
* 当ServletContext对象被创建时,激发contextInitialized(ServletContextEvent sce)方法
* 当ServletContext对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法
ServletRequestListener接口
* ServletRequestListener接口用于监听ServletRequest对象的创建和销毁
* 创建一个ServletRequest对象时,激发requestInitialized(ServletRequestEvent sre)方法
* 销毁一个Session时,激发requestDestroyed(ServletRequestEvent sre)方法。
HttpSessionListener接口
* HttpSessionListener接口用于监听HttpSession对象的创建和销毁
* 创建一个Session时,激发sessionCreated(HttpSessionEvent se)方法
* 销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。
转载于:https://www.cnblogs.com/yangHS/p/11220293.html
相关资源:JAVA上百实例源码以及开源项目