1.首先来看一看源码 该类的源码

  1. public interface ServletContextListener extends EventListener {
  2. /**
  3. * Receives notification that the web application initialization
  4. * process is starting.
  5. *
  6. * <p>All ServletContextListeners are notified of context
  7. * initialization before any filters or servlets in the web
  8. * application are initialized.
  9. *
  10. * @param sce the ServletContextEvent containing the ServletContext
  11. * that is being initialized
  12. */
  13. public void contextInitialized(ServletContextEvent sce);
  14. /**
  15. * Receives notification that the ServletContext is about to be
  16. * shut down.
  17. *
  18. * <p>All servlets and filters will have been destroyed before any
  19. * ServletContextListeners are notified of context
  20. * destruction.
  21. *
  22. * @param sce the ServletContextEvent containing the ServletContext
  23. * that is being destroyed
  24. */
  25. public void contextDestroyed(ServletContextEvent sce);
  26. }

此接口中提供了两个方法,用于监听ServletContext  的创建和销毁,也就是监听ServletContext 的生命周期,可以说成是监听Web 应用的生命周期,当web应用启动后,就会触发ServletContextEvent 事件 当此事件执行时,就会被ServletContextListener 监听器监听到,会调用他的 contextInitialized(ServletContextEvent sce)  方法,通过sce 可以获取ServletContext 实例,初始化一些数据,例如缓存的应用,如,创建数据库连接,读取数据库数据,通过setAttribute(“”,obj) 方法设置数据,然后就是可通过servlet 获取servletContext 的实例,通过getAttribute("") 获取设置的数据

实现代码:

  1. public class MyContextListener implements ServletContextListener {
  2. private ServletContext context = null;
  3. public void contextInitialized(ServletContextEvent event) {
  4. context = event.getServletContext();
  5. User user = DatabaseManager.getUserById(1);
  6. context.setAttribute("user1", user);
  7. }
  8. public void contextDestroyed(ServletContextEvent event) {
  9. User user = (User)context.getAttribute("user1");
  10. DatabaseManager.updateUserData(user);
  11. this.context = null;
  12. }
  13. }

如果是web 项目 最后一步是使 ServletContext 生效,需要在web.xml 中配置监听器,并且web.xml 把它放在正确的WEB-INF/classes目录下,

    1. <listener>
    2. <listener-class>MyServletContextListener</listener-class>
    3. </listener>

ServletContextListener 详解的更多相关文章

  1. Web.xml详解(转)

    这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢迎大家一起讨论. ---题记 一.            Web.xml详解: (一)  web.xml加载过程(步骤) 首 ...

  2. java web.xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

  3. web.xml配置详解之listener

    web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...

  4. 《Tomcat与Java Web开发技术详解》思维导图

    越想构建上层建筑,就越觉得底层基础很重要.补课系列. 书是良心书,就是太基础了,正适合补课. [纯文字版] Tomcat与Java Web开发技术详解 Servlet Servlet的生命周期 初始化 ...

  5. jboss之启动加载过程详解

    今天看了看jboss的boot.log和server.log日志,结合自己的理解和其他的资料,现对jboss的启动和加载过程做出如下总结: boot.xml是服务器的启动过程的日志,不涉及后续的操作过 ...

  6. Mybatis案例超详解(上)

    Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...

  7. Web.xml配置详解(转)

    Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一 ...

  8. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  9. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

随机推荐

  1. PowerDesigner 打印错误

    PowerDesigner打开pdm文件时报“打印错误”(解决)   原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://b ...

  2. 将句子表示为向量(下):基于监督学习的句子表示学习(sentence embedding)

    1. 引言 上一篇介绍了如何用无监督方法来训练sentence embedding,本文将介绍如何利用监督学习训练句子编码器从而获取sentence embedding,包括利用释义数据库PPDB.自 ...

  3. NameError: name 'reload' is not defined

    对于 Python 2.X: import sys reload(sys) sys.setdefaultencoding("utf-8") 对于 <= Python 3.3: ...

  4. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  5. AngularJs分层结构小demo

    后端mvc分层,前端也要分层才对嘛.分层的好处不言而喻.简直太清晰,容易维护.反正清爽的一逼.不信你看. 思路:分为controller层和service层.controller层再提取一个公共的层. ...

  6. 如何设置Ultraedit自动换行

    有时候这会非常麻烦, 要让Ultraedit自动换行请按发下方法: 1. 点击菜单栏的"高级→配置",找到"编辑器→自动换行/制表符设置". 2. 然后,把&q ...

  7. 数据库类型与JDBC TYPE 和Java类型对应关系

    https://blog.csdn.net/seelye/article/details/40105969

  8. bzoj千题计划202:bzoj3191: [JLOI2013]卡牌游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3191 每个人获胜的概率只与其在排列中与庄家的相对位置有关 dp[i][j] 还剩i个人时,从庄家数第 ...

  9. WebSockets Tutorial(教程一)WebSockets简介

    一.WebSockets简介 以字面意思来说,握手可以被定义为两个人抓住和握手右手,象征着问候,祝贺,同意或告别.在计算机科学中,握手是确保服务器与客户端同步的过程.握手是Web Socket协议的基 ...

  10. alloffthelights使用方法

    官网上的demo没有用,因为官网用的jquery是谷歌的CDN,download下来的demo也没有用,因为demo的路径下少了jquery.所以自己写demo的时候要把jquery和alloffth ...