关于J2EE中的Listener监听器
来源:互联网 发布:东莞天助网络怎么样 编辑:程序博客网 时间:2024/05/17 02:02
Listener是基于观察者模式设计的,Listener为Servlet应用程序提供一种控制程序和数据的手段。
Listener的注册
Java Web的listener注册比较简单,因为其监听的对象及事件都是Servlet容器创建的,如果需要它们的监听器的话,只需要在web.xml中配置即可:
对于多个实现相同接口的listener,在执行时按照在web.xml中注册的顺序来决定。
listener也可以在应用程序中动态添加。
Listener相关接口
Servlet规范中定义了多种类型的listener,它们用于监听的事件源分别为ServletContext,HttpSession和ServletRequest这三个域对象。
Servlet规范针对这三个对象上的操作,又把这写监听器分为三种类型:
- 监听三个域对象的创建和销毁的事件监听器
Listener类
含有的接口
接口说明
ServletContextListener
contextInitialized(ServletContextEvent sce);
contextDestroyed(ServletContextEvent sce);
Context容器初始化时触发,在所有的Filter和Servlet的init方法调用之前contextInitialized接口先被调用;
Context容器销毁,在所有的Filter和Servlet的destroy方法调用之后contextDestroyed接口被调用;
HttpSessionListener
SessionCreated(HttpSessionEvent se);
SessionDestroyed(HtppSessionEvent se);
当一个session对象被创建时触发;
当一个session对象被失效时触发;
ServletRequestListener
requestInitialized(ServletRequestEvent sre);
requestDestroyed(ServletRequestEvent sre);
当HttpServletRequest对象被传递到用户的Servlet的service方法之前该方法被触发;
当HttpServletRequest对象在调用完用户的Servlet的service方法之后该方法被触发;
注意:ServletContextListener在容器启动之后就不能再添加新的了,因为它所监听的事件已经不会再出现了。
- 监听三个域对象中属性的增,删,改的事件监听器
Listener类
含有的接口
接口说明
ServletContextAttributeListener
AttributeAdded(ServletContextAttributeEvent scab);
AttributeRemoved(ServletContextAttributeEvent scab);
AttributeReplaced(ServletContextAttributeEvent scab);
当调用servletContext.setAttribute方法时触发这个方法;
当调用servletContext.removeAttribute方法时触发这个方法;
如果在调用servletContext.setAttribute之前该attribute已经存在,则替换这个attribute时,这个方法被触发
HttpSessionAttributeListener
attributeAdded(HttpSessionBindingEvent se);
attributeRemoved(HttpSessionBindingEvent se);
attributeReplaced(HttpSessionBindingEvent se);
session.setAttribute方法被调用时该方法被触发;
session.removeAttribute方法被调用时该方法被触发;
如果在调用session.setAttribute之前该attribute已经存在,则替换这个attribute时这个方法被触发;
ServletRequestAttributeListener
AttributeAdded(ServletRequestAttributeEvent srae);
AttributeRemoved(ServletRequestAttributeEvent srae);
AttributeReplaced(ServletRequestAttributeEvent srae);
当调用request.setAttribute方法时触发这个方法;
当调用request.removeAttribute方法时触发这个方法;
如果在调用request.setAttribute之前该attribute已经存在,则替换attribute时这个方法被触发;
- 感知型监听器
这种监听器不需要注册。
某个java bean实现这个接口后就可以监听何时被绑定,解绑或被激活或钝化。
HttpSessionBindingListener:实现该接口的类,能检测自己何时被HttpSession绑定,解绑;
HttpSessionActivationListener:实现该接口的类,能检测自己何时随着HttpSession一起激活和钝化;
Listener的应用示例
比如:
- 应用的初始化工作 —— 在自定义继承的ServletContextListener监听器的contextInitialized方法中,进行应用级的资源初始化以便提高效率,在contextDestroyed方法中对应用级的资源进行释放;
- 关于J2EE中的Listener监听器
- j2ee Listener 监听器
- J2EE监听器Listener基础
- J2EE:Listener监听器
- java中的监听器(listener)
- javaweb中的Listener监听器
- Java中的Listener监听器
- Java中的Listener 监听器
- J2EE中监听器Listener的应用
- J2EE中监听器Listener的应用
- j2ee中Servlet的八大Listener监听器
- java-WEB中的监听器Listener
- 监听器用法(listener)应用到j2ee项目中
- 监听器(Listener)在开发中的应用
- 监听器(Listener)在开发中的应用
- 监听器(Listener)在开发中的应用(一)
- 监听器(Listener)在开发中的应用(二)
- JSP 中的 Filter 过滤器和 Listener 监听器
- ios 学习资源整理
- verilog基础知识--同步、异步
- Win7部署MVC网站问题
- Java安全(JCA(Java Cryptography Architecture)/JSSE(Java Secure Socket Extension)):基本知识
- MySQL当批量插入遇上唯一索引
- 关于J2EE中的Listener监听器
- IOS SDK不支持64位,编译报错,解决办法
- 灵活运用VS外部工具:Copy assembly to GAC
- 第12周项目3 图遍历算法实现
- MySQL定时器Events
- position & anchorPoint!
- [Android] Android开发优化之——使用软引用和弱引用
- UI之imageView
- iOS动画开发之五——炫酷的粒子效果