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. 各种蕴含算法思想的DP - 2

    study from: https://www.cnblogs.com/flashhu/p/9480669.html 3.斜率dp study from:http://www.cnblogs.com/ ...

  2. c读入实型

    读入: 如果读入的数为整型,然后转为实型,则%lf 否则%f也可以 读出: %f,这样在codeblocks才能看到正确的结果

  3. 使用solrJ操作solr常用方法 【注释非常详细,非常好】

    转: 使用solrJ操作solr常用方法 2017年08月07日 22:49:06 成都往右 阅读数:8990   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  4. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

  5. 20145215《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145215<网络对抗>shellcode注入&Return-to-libc攻击深入 Shellcode注入 基础知识 Shellcode实际是一段代码,但却作为数据发送给受攻 ...

  6. python---django中orm的使用(1)

    首先推荐两篇文章:Django之ORM操作,http://www.cnblogs.com/yuanchenqi/articles/6083427.html十分全面. 另外和python---ORM之S ...

  7. SDWebImage源码阅读-第二篇

    一  SDWebImageManager的downloadImageWithURL的方法 上一篇,我们刚开了个头,分析了一下开始加载图片之前如何取消其他正在下载的任务,接着,我们回到 - (void) ...

  8. HTTP协议之响应头Date与Age

    HTTP没有为用户提供一种手段来区分响应是缓存命中的,还是访问原始服务器得到的.客户端有一种方法能判断响应是否来自缓存,就是使用Date首部.将响应中Date首部的值与当前时间进行比较,如果响应中的日 ...

  9. python selenium - web自动化环境搭建

    前提: 安装python环境. 参考另一篇博文:https://www.cnblogs.com/Simple-Small/p/9179061.html web自动化:实现代码驱动浏览器进行点点点的操作 ...

  10. 【黑客免杀攻防】读书笔记10 - switch-case分支

    0x1 switch-case分支 switch-case其实就是if-else语句的另一种体现形式.但大于3之后的switchc-case.编译器会对代码进行优化. 1.1 简单switch-cas ...