ServletContextListener 详解
1.首先来看一看源码 该类的源码
- public interface ServletContextListener extends EventListener {
- /**
- * Receives notification that the web application initialization
- * process is starting.
- *
- * <p>All ServletContextListeners are notified of context
- * initialization before any filters or servlets in the web
- * application are initialized.
- *
- * @param sce the ServletContextEvent containing the ServletContext
- * that is being initialized
- */
- public void contextInitialized(ServletContextEvent sce);
- /**
- * Receives notification that the ServletContext is about to be
- * shut down.
- *
- * <p>All servlets and filters will have been destroyed before any
- * ServletContextListeners are notified of context
- * destruction.
- *
- * @param sce the ServletContextEvent containing the ServletContext
- * that is being destroyed
- */
- public void contextDestroyed(ServletContextEvent sce);
- }
此接口中提供了两个方法,用于监听ServletContext 的创建和销毁,也就是监听ServletContext 的生命周期,可以说成是监听Web 应用的生命周期,当web应用启动后,就会触发ServletContextEvent 事件 当此事件执行时,就会被ServletContextListener 监听器监听到,会调用他的 contextInitialized(ServletContextEvent sce) 方法,通过sce 可以获取ServletContext 实例,初始化一些数据,例如缓存的应用,如,创建数据库连接,读取数据库数据,通过setAttribute(“”,obj) 方法设置数据,然后就是可通过servlet 获取servletContext 的实例,通过getAttribute("") 获取设置的数据
实现代码:
- public class MyContextListener implements ServletContextListener {
- private ServletContext context = null;
- public void contextInitialized(ServletContextEvent event) {
- context = event.getServletContext();
- User user = DatabaseManager.getUserById(1);
- context.setAttribute("user1", user);
- }
- public void contextDestroyed(ServletContextEvent event) {
- User user = (User)context.getAttribute("user1");
- DatabaseManager.updateUserData(user);
- this.context = null;
- }
- }
如果是web 项目 最后一步是使 ServletContext 生效,需要在web.xml 中配置监听器,并且web.xml 把它放在正确的WEB-INF/classes目录下,
- <listener>
- <listener-class>MyServletContextListener</listener-class>
- </listener>
ServletContextListener 详解的更多相关文章
- Web.xml详解(转)
这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢迎大家一起讨论. ---题记 一. Web.xml详解: (一) web.xml加载过程(步骤) 首 ...
- java web.xml配置详解(转)
源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...
- web.xml配置详解之listener
web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...
- 《Tomcat与Java Web开发技术详解》思维导图
越想构建上层建筑,就越觉得底层基础很重要.补课系列. 书是良心书,就是太基础了,正适合补课. [纯文字版] Tomcat与Java Web开发技术详解 Servlet Servlet的生命周期 初始化 ...
- jboss之启动加载过程详解
今天看了看jboss的boot.log和server.log日志,结合自己的理解和其他的资料,现对jboss的启动和加载过程做出如下总结: boot.xml是服务器的启动过程的日志,不涉及后续的操作过 ...
- Mybatis案例超详解(上)
Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...
- Web.xml配置详解(转)
Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
随机推荐
- #include<iostream>与#include<iostream.h>以及#inclue<string> 和 #include<string.h>的区别
转载于:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1900715.html 1.从功能性的角度来讲,<iostream> ...
- 弹指之间 -- Slow Soul
CHAPTER 16 慢灵魂乐 Slow Soul (8Beat) Slow Soul每小节内几乎都是以8分音符弹奏,又称之为8Beat节奏,80左右的速度最能表现此节奏特色. 示例曲目: 拥抱
- 解决 django 博客归档 “Are time zone definitions for your database and pytz installed?”的错误
修改 project 中的settings 文件,问题解决! # USE_TZ = True USE_TZ = False # LANGUAGE_CODE = 'en-us' LANGUAGE_COD ...
- [应用篇]第六篇 JSTL之自定义函数标签库
在之前的JSTL的总结中已经对函数标签库进行了一些说明,在这里我再一次重新整理一下! 自带函数标签库介绍 引入该标签库的方法为: <%@ taglib prefix="fn&quo ...
- POJ - 3020 Antenna Placement(最小覆盖路径)
---恢复内容开始--- https://vjudge.net/problem/POJ-3020 题意 *--代表城市,o--代表空地 给城市安装无线网,一个无线网最多可以覆盖两座城市,问覆盖所有城市 ...
- hdu 527 Necklace
http://acm.hdu.edu.cn/showproblem.php?pid=5727 阶乘 爆搜阴性宝石的排列,二分图最大匹配判断最多能使多少个阳性宝石不褪色 注: 1.O(n-1 !) 即可 ...
- Nginx ab压力测试
20-ab压力测试及nginx性能统计模块 优化的启发,打开的文件太多 Nginx 错误日志显示,打开文件数太多 系统层面 more /proc/sys/net/core/somaxconn 单个Ng ...
- php-fpm的status可以查看汇总信息和详细信息
nginx.conf 配置文件 server { listen ; server_name localhost; index index.php index.html; root /home/tiny ...
- Stochastic Optimization Techniques
Stochastic Optimization Techniques Neural networks are often trained stochastically, i.e. using a me ...
- Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式
Mapper代理的开发规范 1. mapper接口的全限定名要和mapper映射文件的namespace值一致. 2. mapper接口的方法名称要和mapper映射文件的statement的id一致 ...