web Listener介绍
来源:互联网 发布:php源码怎么本地测试 编辑:程序博客网 时间:2024/06/08 16:57
当web运用在web容器中运行时,会产生各种事件(如web启动,停止,用户的session创建,销毁,请求到达等)这些对于web事件开发者都可以监听到。
使用Listener只需要2个步骤
1)定义Lisenter实现类
- package com.listener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import org.apache.log4j.*;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import com.quartz.QuartzServiceImpl;
- import com.util.DateUtil;
- public class WebStartListener implements ServletContextListener{
- Logger logger = Logger.getLogger(WebStartListener.class);
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- // TODO Auto-generated method stub
- logger.warn("启动");
- // ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:com/springResource/*.xml"});
- // QuartzServiceImpl quartzService = (QuartzServiceImpl)springContext.getBean("quartzService");
- // quartzService.schedule("name=testQuartz2",DateUtil.parse("2012-12-12 23:42:00"));
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- // TODO Auto-generated method stub
- logger.warn("关闭");
- }
- }
2)配置web.xml
- <!-- web服务器启动监听 -->
- lt;listener>
- <listener-class>com.listener.WebStartListener</listener-class>
- lt;/listener>
servlet内置listener
l ServletContextListener
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
l ServletContextAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletContextAttributeEvent
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
l HttpSessionListener
[接口方法] sessionCreated()与sessionDestroyed ()
[接收事件] HttpSessionEvent
[触发场景] 在session(HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
l HttpSessionAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
l ServletRequestListener
[接口方法] requestInitialized()与 requestDestroyed()
[接收事件] RequestEvent
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
l ServletRequestAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
l HttpSessionBindingListener
[接口方法] valueBound()与valueUnbound()
[接收事件] HttpSessionBindingEvent
[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。
l HttpSessionActivationListener
[接口方法] sessionDidActivate()与 sessionWillPassivate()
[接收事件] HttpSessionEvent
[触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。
- Web listener 介绍
- web Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Listener介绍
- Servlet介绍(6)-----listener
- Servlet介绍-listener
- Servlet介绍(6)-----listener
- listener监听器介绍
- Servlet介绍-listener
- 2.13 Listener介绍
- Activiti入门——轻松解读数据库
- 三层数据库四大法宝之“增”
- Ubuntu下搭建 Eclipse+NDK for Android JNI 的开发环境
- yii2 用命令行操作web下的controller
- PADS router使用技巧之飞线设置
- web Listener介绍
- C/ObjC不定参数与哨兵参数
- 每天进步一点点——负载均衡之DNS域名解析
- Nginx+tomcat负载均衡session问题解决
- 《蒹葭》-- 选自《诗经·国风·秦风》
- Java项目中的分包
- APK动态加载框架DL解析
- spring集成的邮件发送,可发送附件和富文本
- Tcl在Vivado中的应用