<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介绍的更多相关文章

  1. ServletConfig与ServletContext

    ServletConfig与ServletContext对象详解 一.ServletConfig对象    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...

  2. ServletConfig和ServletContext

    ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...

  3. JavaEE:Servlet简介及ServletConfig、ServletContext

    Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...

  4. day05 Servlet 开发和 ServletConfig 与 ServletContext 对象

    day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...

  5. JaveWeb学习之Servlet(二):ServletConfig和ServletContext

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...

  6. ServletConfig、ServletContext 的应用

    一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...

  7. JavaWeb学习笔记:ServletConfig()和ServletContext()

    ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...

  8. ServletConfig和ServletContext 区别

      ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...

  9. 谈谈 ServletConfig 和 ServletContext

    目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...

随机推荐

  1. hdoj--1829--A Bug's Life(带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. C#编译器优化

    C#编译器优化 https://www.cnblogs.com/podolski/p/8987595.html 使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置 ...

  3. [源码管理] ubuntu中svn简明用法:服务器搭建+客户端使用

    本文是对网络上前人的优秀文章加以实践验证后所整理(修正或补充) 第一部分:svn服务器搭建(主要是四步走) 参考:http://www.son1c.cn/show/920.html 一,安装Subve ...

  4. APM技术原理

    链接地址:http://www.infoq.com/cn/articles/apm-Pinpoint-practice 1.什么是APM? APM,全称:Application Performance ...

  5. 使用新的CSS类型对象模型

    el.attributeStyleMap.set('padding', CSS.px(42)); const padding = el.attributeStyleMap.get('padding') ...

  6. Swift 字典 Dictionary基本用法

    import UIKit /* 字典的介绍 1.字典允许按照某个键访问元素 2.字典是由两部分组成, 一个键(key)集合, 一个是值(value)集合 3.键集合是不能有重复的元素, 值集合可以有重 ...

  7. 【数据分析】算法+Echarts小练

    ''' 处理逻辑: 按number去处理 先遍历所有的number挨个去找有没有在列表里的,在列表里的拿出另外一个append 把number去除的列表 ''' li = [] with open(r ...

  8. 网络教程(7)OSI模型的低层模型

    OSI Model——Open System Interconnection Model 开放系统互联模型

  9. 分类IP地址

    分类IP地址是将IP地址划分为若干个固定类. IP地址由网络标识字段(netID)和主机标识字段(hostID)组成.IP地址可以标识为: IP地址:: = { <网络标识>,<主机 ...

  10. C#常用 API函数大全

    常用Windows API1. API之网络函数WNetAddConnection 创建同一个网络资源的永久性连接WNetAddConnection2 创建同一个网络资源的连接WNetAddConne ...