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 ...
随机推荐
- poj--2186--Popular Cows (scc+缩点)
Popular Cows Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- 【SDOI 2010】 计算器
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2242 [算法] 第一问用快速幂解决 第二问用exgcd解决 第三问用BSGS算法解决 ...
- Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式
ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...
- 如何提升SQL语句的查询性能
在对数据库进行操作时,如果SQL语句书写不当,对程序的效率会造成很大影响. 提高SQL效率可以从一下几个方面入手: 1,数据库设计与规划 Primary Key字段的长度尽量小,能用small int ...
- 23. Merge k Sorted Lists[H]合并k个排序链表
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- E - Dividing Orange
Problem description One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments ...
- HBase编程 API入门系列之工具Bytes类(7)
这是从程度开发层面来说,为了方便和提高开发人员. 这个工具Bytes类,有很多很多方法,帮助我们HBase编程开发人员,提高开发. 这里,我只赘述,很常用的! package zhouls.bigda ...
- 将我们的parser转换成Monad
还记得我们上一篇delegate类型的parser吗 ,在开始本篇之前,强烈建议你复习一下这个parser定义 public delegate Maybe<Tuple<T,string& ...
- 配置postgreSQL允许外部连接
配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改 %PostgreSQL_path%/data 目录下的 pg_hba.conf 和 postgresql.conf. 一.修改pg_h ...
- 僧多粥少?还原 OpenStack 的真实“钱景”
原文链接:http://www.oschina.net/news/57994/openstack-income-analysis 451 Research发布了OpenStack的收入分析预测,指出O ...