JSP/Servlet 中的事件处理写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应。
其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。
1. Servlet中的Listener和Event:
在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。
ServletContextListener接口
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
ServletContextAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletContextAttributeEvent
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
HttpSessionListener
[接口方法] sessionCreated()与sessionDestroyed ()
[接收事件] HttpSessionEvent
[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
HttpSessionAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
HttpSessionActivationListener
[接口方法] sessionDidActivate()与 sessionWillPassivate()
[接收事件] HttpSessionEvent
[触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。
ServletRequestListener
[接口方法] requestInitialized()与 requestDestroyed()
[接收事件] RequestEvent
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
ServletRequestAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
HttpSessionBindingListener
[接口方法] valueBound()与valueUnbound()
[接收事件] HttpSessionBindingEvent
[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。
2. 如何注册Servlet中的事件
实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如:
< listener >
< listener-class > demo.servlet.listener.CustomServletContextListener
3. Servlet事件的应用实例
看到这里,你也许会有疑问: 了解这些 listener和event 有什么用呢?我平时开发没有用到这些,一样也能完成任务啊.
不错,在日常的开发中很少用到这些事件处理的方面,但是在某些情况下使用事件处理机制却可以达到事半功倍的效果,例如下面两个例子:
4.Java类实例
==========
//侦听启动和关闭
import javax.servlet.ServletContextListener;
import javax.servlet.*;
public class TigerListen implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce)
{
System.out.print("Init") ;
}
public void contextDestroyed(ServletContextEvent sce)
{
System.out.print("Destroved") ;
}
}
对应的web.xml是
============
<!----><listener></listener>
<listener-class></listener-class> TigerListen
============
分享到:
相关推荐
详细的解释了servlet filter listener 额知识,本文档适合初学者阅读
servlet_filter_listener
Servlet、Filter、Listener深入理解.docx
关于filter、servlet在web.xml配置及加载顺序
Java WEB 篇九 Java servlet、filter、listener、interceptor 之间的区别和联系?
servlet--listener 各种接口的简单例子 ,将每个接口的实现都简单的举例,特别容易学会,下载后直接 放到工作空间后运行
本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢
特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象,本文源码下载地址http://usc.googlecode.com/files/UsersManagerWithAJAX.rar
web.xml 中的listener、 filter、servlet 加载顺序及其详解
JAVA Servlet监听器listener学习资料,精心整理,望对各位大虾有帮助。
web服务器三大组件servlet、Filter、Listene——浅浅笔记
关于所有ServletListener的介绍,非常有用的一个小东东
本篇文章主要介绍了SpringBoot初始教程之Servlet、Filter、Listener配置详解,具有一定的参考价值,有兴趣的可以了解一下
给大家介绍了spring-boot中如何定义过滤器、监听器和拦截器,对Spring Boot 编写Servlet、Filter、Listener、Interceptor的相关知识感兴趣的朋友一起看看吧
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ServletContextListener (具体实现哪个Listener根据情况来判断) 2.在类上加入注解 @WebListener 3.重写 context...
在Servlet 3.0之前都是使用web.xml文件进行配置,这篇文章主要介绍了SpringBoot如何注册Servlet、Filter、Listener的几种方式,在Servlet 3.0之前都是使用web.xml文件进行配置,
主要介绍了Java servlet、filter、listener、interceptor之间的区别和联系的相关资料,需要的朋友可以参考下