ServletContext : 每一个web应用都有一个 ServletContext与之相关联。 ServletContext对象在应用启动的被创建,在应用关闭的时候被销毁。 ServletContext在全局范围内有效,类似于应用中的一个全局变量。

ServletContextListener: 使用listener接口,开发者能够在为客户端请求提供服务之前向ServletContext中添加任意的对象。这个对象在ServletContext启动的时候被初始化,然后在ServletContext整个运行期间都是可见的。该接口拥有两个方法如下所示:

 void contextDestoryd(ServletContextEvent sce);
void contextInitialized(ServletContextEvent sce);

用户需要创建一个java类实现 javax.servlet.ServletContextListener接口并提供上面两个方法的实现。

示例: 当你需要在处理任何客户端请求之前创建一个数据库连接,并且希望在整个应用过程中该连接都是可用的,这个时候ServletContextListener接口就会十分有用了。

 package com.database;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributesListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.database.DbConnection; public class DatabaseContextListener implements ServletContextListener { private ServletContext context = null;
private Connection conn = null; public DatabaseContextListener() { } //该方法在ServletContext启动之后被调用,并准备好处理客户端请求
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
conn = DbConnection.getConnection;
// 这里DbConnection是一个定制好的类用以创建一个数据库连接
context = setAttribute(”dbConn”,conn);
} //这个方法在ServletContext 将要关闭的时候调用
public void contextDestroyed(ServletContextEvent event){
this.context = null;
this.conn = null;
}
}

然后部署该类,并在web.xml文件中添加

 <listener>
com.database.DatabaseContextListener
</listener>

一旦web应用启动的时候,我们就能在任意的servlet或者jsp中通过下面的方式获取数据库连接:

 Connection conn = (Connection) getServletContext().getAttribute(”dbConn”);

怎样使用ServletContextListener接口的更多相关文章

  1. ServletContextListener接口用法

    ServletContextListener接口用于tomcat启动时自动加载函数,方法如下: 一.需加载的类必须实现ServletContextListener接口. 二.该接口中有两个方法必须实现 ...

  2. ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题

    想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...

  3. 通过实现ServletContextListener接口创建数据库连接池(C3P0方式)

    使用Listener步骤 1. 定义Listener实现类 2. 在web.xml中配置(或使用Annotation) 使用C3P0方式创建数据库连接池需要添加的jar包 1.c3p0-0.9.5.j ...

  4. ServletContextListener使用详解

    在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...

  5. ServletContentLIstener接口演示ServletContext的启动和初始化

    ServletContextListener接口中包含两个方法,一个是contextInitialized()方法, 用来监听ServletContext的启动和初始化:一个是contextDestr ...

  6. ServletContextListener

    在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...

  7. Spring的核心接口

    ContextLoaderListener接口 Create a new ContextLoaderListenerthat will create a web application context ...

  8. 使用ServletContextListener关闭Redisson连接

     ServletContextListener 监听器 在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周 ...

  9. Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证

    本文通过一个简易安全认证示例的开发实践,理解过滤器和拦截器的工作原理. 很多文章都将过滤器(Filter).拦截器(Interceptor)和监听器(Listener)这三者和Spring关联起来讲解 ...

随机推荐

  1. Removing Columns 分类: 贪心 CF 2015-08-08 16:10 10人阅读 评论(0) 收藏

    Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. 从hadoop框架与MapReduce模式中谈海量数据处理

    http://blog.csdn.net/wind19/article/details/7716326 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显 ...

  3. 如何快捷输入函数上方的注释代码(Summary)

    写完类或函数(注意必须写完,不然出现的信息会不完整)后,在其上方空行输入/**,然后回车,就可以为其添加Summary.    

  4. linux poll 学习

    一.poll介绍 函数原型: #include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout); struc ...

  5. ubuntu下环境变量

    01:/etc/environment:是设置整个系统的环境: 02:/etc/profile:是设置所有用户的环境: 03:.bashrc :本地用户:

  6. javascript中set与get方法详解

    其中get与set的使用方法: 1.get与set是方法,因为是方法,所以可以进行判断. 2.get是得到 一般是要返回的   set 是设置 不用返回 3.如果调用对象内部的属性约定的命名方式是_a ...

  7. 2014江西理工大学C语言程序竞赛初级组

    坐公交 解法:略 #include<stdio.h> #include<string> #include<iostream> #include<math.h& ...

  8. C#实现ActiveX控件开发与部署

    现在,我们手里已经有了这个控件包:QRCode.cab,下面我们编写一个测试的网页 <html> <head> <title>无标题页</title> ...

  9. Utility

    #region Tools public static string ForMatDateTime(DateTime dt, string formatStr) { return string.For ...

  10. Hibernate 查询语句

    Hibernate查询语言(HQL)是一种面向对象的查询语言,类似于SQL,但不是对表和列操作,HQL适用于持久对象和它们的属性. HQL查询由Hibernate转换成传统的SQL查询,这在圈上的数据 ...