<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. LinkedHashMap做缓存

    项目上需要写一个缓存,这样就不需要频繁地访问数据库,我使用的是 //缓存 private final Map<String, JSONArray> schemaCache = new Li ...

  2. Cracking the Coding Interview 5.2

    Given a(decimal -e.g. 3.72)number that is passed in as a string, print the binary representation. If ...

  3. 介绍一个简单的Parser

    我们已经学习了怎样创建一个简单的Monad, MaybeMonad, 并且知道了它如何通过在 Bind函数里封装处理空值的逻辑来移除样板式代码. 正如之前所说的,我们可以在Bind函数中封装更复杂的逻 ...

  4. vue1.0.js的初步学习

    vue.js是一个mvvm框架 {{.....}}   常用模板渲染方式 v-model  :将对应变量的值的变化反映到input的vaule中 vue.js 的一个组件 .vue文件包含<te ...

  5. jQuery操作DOM知识总结

    jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...

  6. Python学习笔记基础篇-(1)Python周边

    一.系统命令 1.Ctrl+D 退出Python IDLE input方法中输入EOF字符,键入Ctrl+D 2.命令行选项: -d   提供调试输出 -O 生成优化的字节码(.pyo文件) -S 不 ...

  7. OpenCV中GPU模块使用

    CUDA IT168的文章系列: Cuda的初始化:http://tech.it168.com/a2011/0715/1218/000001218458.shtml OpenCV: OpenCV中GP ...

  8. Python基础:使用list & tuple

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. tuple 小结 list和tuple是Python内置的有序集合,一个可变,一个不 ...

  9. 配置H3C交换机ftp服务

    配置H3C交换机ftp服务,用于与交换机进行文件上传.下载,常用于更新程序上传及配置备份文件下载. 准备工作:三层设备(路由器.三层交换机等)至少一个接口配置IP,二层交换机需配置一个处于UP状态的v ...

  10. 任务调度开源框架Quartz概述

    任务调度开源框架Quartz 几乎每个项目中都用到了自动任务处理功能.所以在任务调度的功能很常用,但是一个好的任务调度程序是一个颇具挑战性的工作.最近用到Quartz这个框架,感觉很好,所以进行学习. ...