servletConfig和ServletContext 以及servletContextListener介绍
<servlet>
<servlet-name>BeerParamTests</servlet-name>
<servlet-class>TestInitParams</servlet-class>
<init-param>
<param-name>addEmail</param-name>
<param-value>lik@qq.com</param-name>
</init-param>
</servlet>
在servlet代码中能够通过
getServletConfig().getInitParameter("addEmail"):
调用getServletConfig() 不能在初始化參数之前调用 不能在init中调用 必须等servlet载入完才干够调用.
********容器初始化一个servlet时,会为这个Servlet创建一个唯一的ServletConfig********
创建servletConfig步骤:
1.容器读取Servlet部署文件,包含servlet的初始化參数(init-param)
2.容器为这个servlet创建一个新的servletConfig实例
3.容器为每一个servlet初始化參数创建一个键值对。
4.容器向ServletConfig提供键值对初始化參数的引用.
5.容器创建servlet类的一个新实例
6.容器调用servlet的init方法 传入servletCOnfig的引用.
容器仅仅会读一次Servlet初始化參赛,在Servlet生命周期中无法改变这个值,除非又一次部署servlet.
上下文初始化參数,不不过针对一个Servlet可用。它是针对整个web应用,应用中全部Servlet和jsp都自己主动的能訪问
上下文初始化參数。能够在web.xml中配置。
<servlet>
</servlet>
<context-param>
<param-name>add</param-name>
<param-value></param-value>
</context-param>
在代码中,getServletContext().getInitParameter("add");
或者ServletContext context = getServletContext();
context.getInitParameter("add");
每一个servlet一个ServletConfig
每一个web应用一个ServletContext
假设web应用是分布的,那么每一个jvm就会有一个servletcontext,假设应用分布在多个server上
那么web应用实际上能够有多个servletContext。
一个ServletContext确实仅仅相应一个应用。可是前提是应用
在一个jvm中.
servletContext和ServletConfig仅仅有获取的方法,没有设置的值的方法。都是常量。
两种方法获取ServletContext对象
getServletConfig().getServletContext().getInitParameter();
或者
this.getServletConfig().getServletContext();
ServletContextListener 监听ServletContext一生中关键两个事件。初始化和撤销.
ServletContextListener能够做的工作:
1.上下文初始化时候的能够做:从servletContext得到上下文初始化參赛。
使用初始化參数查找名建立一个数据库连接
把数据库连接存储为一个属性,使得web应用各个部分都能訪问
上下文撤销时:
关闭数据库连接
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event){
//数据库连接代码
}
public void contextDestroyed(ServletContextEvent event){
//关闭数据库连接代码
}
}
以下是一个应用实例对与监听器的使用
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event){
//数据库连接代码
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog",d);//利用上下文设置属性,在其它地方就能够获得属性
}
public void contextDestroyed(ServletContextEvent event){
//关闭数据库连接代码
}
}
public class Dog{
private String breed;
public Dog(String breed){
this.breed = breed;
}
public String getBreed(){
return breed;
}
}
public class ListenerTester extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws Excepton{
response.setContentType("text/html");\
PrintWriter out = response.getWriter();
Dog dog = (Dog)getServletContext().getAttribute("dog");
out.println(dog.getBreed());
}
}
web.xml部署文件
<servlet>
</servlet>
<servlet-mapping>
</servlet-mapping>
<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
<listener>
<listener-class>
com.gac.MyServletContextListener
</listener-class>
</listener>
servletConfig和ServletContext 以及servletContextListener介绍的更多相关文章
- ServletConfig与ServletContext
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
- ServletConfig和ServletContext
ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- ServletConfig、ServletContext 的应用
一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...
- JavaWeb学习笔记:ServletConfig()和ServletContext()
ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...
- ServletConfig和ServletContext 区别
ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...
- 谈谈 ServletConfig 和 ServletContext
目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...
随机推荐
- hdoj--1258--Sum It Up(dfs)
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Gym-101915B Ali and Wi-Fi 计算几何 求两圆交点
题面 题意:给你n个圆,每个圆有一个权值,你可以选择一个点,可以获得覆盖这个点的圆中,权值最大的m个的权值,问最多权值是多少 题解:好像是叙利亚的题....我们画画图就知道,我们要找的就是圆与圆交的那 ...
- HBase编程 API入门系列之delete.deleteColumn和delete.deleteColumns区别(客户端而言)(4)
心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. delete.deleteColumn和delete.deleteColumns区别是: deleteColumn是删除某一个列簇 ...
- winfrom控件——基本工具
窗体事件:属性—事件—load(双击添加) 窗体加载完之后的事件: 删除事件:先将属性事件里挂号的事件名删掉(行为里的load)再删后台代码里的事件. 控件:工具箱里(搜索—双击或点击拖动到窗体界面) ...
- 第7章 性能和可靠性模式 Server Clustering(服务器群集)
上下文 您正在设计要部署应用程序的基础结构层.运行要求包括无法满足的可用性或性能能力,因为基础结构中存在性能瓶颈或故障单点. 影响因素 设计基础结构时,请考虑下列影响因素: 用户希望在使用应用程序时这 ...
- 洛谷P4413 [COCI2006-2007#2] R2(可持久化平衡树维护NTT)
题意翻译 设S=(R1+R2)/2,给定R1与S (-1000<=R1,S<=1000)(−1000<=R1,S<=1000) ,求R2. 感谢@Xeonacid 提供的翻译 ...
- GoogleMap 获取自己的数字证书API key的步骤
http://dreamylights.blog.51cto.com/1163218/1360759 1. 进入到Google APIs Console页面 https://code.google.c ...
- (转载) android studio library生成jar包和aar的方法总结
android studio library生成jar包和aar的方法总结 标签: android学习文档jar和aar的使用与生成gradle 2016-11-25 10:39 1782人阅读 评论 ...
- MSSQL_20160719_在作业步骤中使用sp_send_dbmail遇到的问题
需求: 在作业步骤中使用sp_send_dbmail发出邮件, 并将数据库中的日志表通过@query参数导出文本作为邮件附件 遇到错误: 服务器 DB-DWH-1,第 1 行 服务器主体 " ...
- redis BIO详解
BIO即background I/O service,后台I/O服务,是redis的aof持久化后台服务. redis把阻塞的同步I/O操作交给后台I/O服务来完成:close和fsync. clos ...