<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. Eclipse-Error:笔记-1

    ylbtech-Eclipse-Error:笔记-1 1.返回顶部 1. Whitelabel Error PageThis application has no explicit mapping f ...

  2. [JavaEE] Spring学习资源链接

    学习规划 http://forrest-lv.iteye.com/blog/1166947 SpringMVC - 框架配置 http://blog.csdn.net/ljhabc1982/artic ...

  3. wampserver配置多站点

    1.打开C:\wamp\bin\apache\apache2.2.22\conf\httpd.conf(因安装的路径而异),查找listen 80 下面加上listen 8080 2.然后加上 < ...

  4. JDBC基础01

    今日知识 1. JDBC基本概念2. 快速入门3. 对JDBC中各个接口和类详解 JDBC: 1. 概念:Java DataBase Connectivity Java 数据库连接, Java语言操作 ...

  5. Mysql数据库概述

    阅读目录 引擎介绍 表介绍 创建表 查看表结构 mysql中的数据类型 表的完整性约束 修改表结构 删除表 多表结构的创建与分析 练习 返回顶部 引擎介绍 mysql中的存储引擎(https://ww ...

  6. Python中断言与异常的区别

    异常,在程序运行时出现非正常情况时会被抛出,比如常见的名称错误.键错误等. 异常: >>> s Traceback (most recent call last): File &qu ...

  7. lua队列实现

    Queue = {} function Queue.newquene() } end function Queue.push(queue, value) queue.count = queue.cou ...

  8. [ Database ] [ SQL Server ] SQL Server 很多不允許的操作解決方法

    說明可參考 https://blog.miniasp.com/post/2012/10/26/SQL-Server-Management-Studio-Prevent-saving-changes-t ...

  9. 模拟试题C

    模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...

  10. 数据的图表统计highcharts

    数据统计常用的图表一般是饼状图.柱状图.线状图,HighCharts可以很好的实现. HighCharts highcharts是基于jquery的一个功能强大的插件,使用时先导入jquery.js ...