怎样使用ServletContextListener接口
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接口的更多相关文章
- ServletContextListener接口用法
ServletContextListener接口用于tomcat启动时自动加载函数,方法如下: 一.需加载的类必须实现ServletContextListener接口. 二.该接口中有两个方法必须实现 ...
- ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题
想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...
- 通过实现ServletContextListener接口创建数据库连接池(C3P0方式)
使用Listener步骤 1. 定义Listener实现类 2. 在web.xml中配置(或使用Annotation) 使用C3P0方式创建数据库连接池需要添加的jar包 1.c3p0-0.9.5.j ...
- ServletContextListener使用详解
在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...
- ServletContentLIstener接口演示ServletContext的启动和初始化
ServletContextListener接口中包含两个方法,一个是contextInitialized()方法, 用来监听ServletContext的启动和初始化:一个是contextDestr ...
- ServletContextListener
在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...
- Spring的核心接口
ContextLoaderListener接口 Create a new ContextLoaderListenerthat will create a web application context ...
- 使用ServletContextListener关闭Redisson连接
ServletContextListener 监听器 在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周 ...
- Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证
本文通过一个简易安全认证示例的开发实践,理解过滤器和拦截器的工作原理. 很多文章都将过滤器(Filter).拦截器(Interceptor)和监听器(Listener)这三者和Spring关联起来讲解 ...
随机推荐
- Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21851 Accepted: 8984 Sp ...
- Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...
- I Think I Need a Houseboat 分类: POJ 2015-06-11 17:52 12人阅读 评论(0) 收藏
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 92090 Acce ...
- selenium帮助手册以及 webdriver的各种driver
帮助手册 http://selenium-python.readthedocs.io/locating-elements.html 转载于:http://blog.csdn.net/five3/art ...
- IOSanimationDidStop
-animationDidStop:finished: 方法中的flag参数表明了动画是自然结束还是被打断,我们可以在控制台打印出来.如果你用停止按钮来终止动画,它会打印NO,如果允许它完成,它会打印 ...
- 使用MSCOMM发送任意文件,还有一些注意事项
第一步:发送文件 FILE* pSENDFILE = _wfopen(m_edit_chosefile, _T("rb"));//以二进制打开待发送文件的的文件指针 fseek(p ...
- SqlSever基础 union 与 union all的区别,即 重复项是否全部显示
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- winfrom增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- thinkphp的自动完成功能说明
手册里有一句话很关键: 自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理. 这句话说明自动完成发生的时间是create()组建数 ...
- Node.js 事件循环(Event Loop)介绍
Node.js 事件循环(Event Loop)介绍 JavaScript是一种单线程运行但又绝不会阻塞的语言,其实现非阻塞的关键是“事件循环”和“回调机制”.Node.js在JavaScript的基 ...