JavaWeb—监听器

简介:

监听器:专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。

Servlet监听器:它用于监听web应用程序中ServletContext、HttpSession、ServletRequest等域对象的创建和销毁事件,以及监听这些域对象中的属性发生修改的事件。

Servlet监听器的分类:

按监听的事件类型Servlet监听器可分为如下三种类型:

—监听域对象自身的创建和销毁的事件监听器

—监听域对象中属性的增加和删除的事件监听器

—监听绑定到HttpSession域中的某个对象的状态的事件监听器

 

监听域对象的创建和销毁:

* 域对象创建和销毁的事件监听器就是用来监听ServletContext,HttpSession,HttpServletRequest这三个对象的创建和销毁事件的监听器。

* 域对象的创建和销毁时机

  创建时机 销毁时机
ServletContext web服务器启动时为每个web应用程序创建相应的ServletContext对象 web服务器关闭时为每个web应用程序销毁相应的ServletContext对象
HttpSession 浏览器开始与服务器会话时创建 调用HttpSession.invalidate(),超过了session的最大有效时间间隔,服务器进程被停止
ServletRequest 每次请求开始时创建 每次访问结束后销毁

 

ServletContextListener接口

* 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)方法。

 

posted @ 2019-07-21 09:54  鸿森  阅读(181)  评论(0编辑  收藏  举报