servlet 八个Listener接口,六个Event类别 (转)

来源:互联网 发布:excel筛选数据后求和 编辑:程序博客网 时间:2024/09/21 06:19



 

 

Servlet事件监听器
在Servlet技术中已经定义了一些事件,并且我们可以针对这些事件来编写相关的事件监听器,从而对事件作出相应处理。Servlet事件主要有3类:Servlet上下文事件、会话事件与请求事件。下面具体讲解这3类事件的监听器实现。

 

1.对Servlet上下文进行监听

 

可以监听ServletContext对象的创建和删除以及属性的添加、删除和修改等操作。该监听器需要使用到如下两个接口类:

● ServletContextAttributeListener:监听对ServletContext属性的操作,如增加、删除、修改操作。

● ServletContextListener:监听ServletContext,当创建ServletContext时,激发contextInitialized (ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContext- Event sce)方法。

 

 

2.监听Http会话

 

可以监听Http会话活动情况、Http会话中属性设置情况,也可以监听Http会话的active、paasivate情况等。该监听器需要使用到如下多个接口类:

● HttpSessionListener:监听HttpSession的操作。当创建一个Session时,激发session Created (SessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)    方法。

● HttpSessionActivationListener:用于监听Http会话active、passivate情况。

● HttpSessionAttributeListener:监听HttpSession中属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

 

 

3.对客户端请求进行监听

 

对客户端的请求进行监听是在Servlet 2.4规范中新添加的一项技术,使用的接口类如下:

● ServletRequestListener接口类。

● ServletRequestAttrubuteListener接口类。

0 0
原创粉丝点击