ServletConfig与ServletContext对象详解

一、ServletConfig对象
    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。(配置在某个servlet标签或者整个web-app下)

当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

首先,需要创建私有变量:private ServletConfig config = null;

其次,要重写init方法,传入config,令this.config = config;从而获得ServletConfig对象

最后,就可以获得<init-parm>中的配置信息了

 public class Snippet
{
//获取初始化参数
String value1 = this.config.getInitParameter("x1");
//获得配置文档中<init-param>标签下name对应的value
String vlaue2 = this.config.getInitParameter("x2");
//2.获取所有的初始化参数(用Enumeration接收)
Enumeration e = this.config.getInitParameterNames();
while(e.hasMoreElements())
{
String name = (String) e.nextElement();
String value = this.config.getInitParameter(name);
System.out.println(name + "=" + value);
}
}

在开发中ServletConfig的作用有如下三个:

1)获得字符集编码

String charset = this.config.getInitParameter("charset");
2)获得数据库连接信息

String url = this.config.getInitParameter("url");
  String username = this.config.getInitParameter("username");
  String password = this.config.getInitParameter("password");
3)获得配置文件

String configFile = this.config.getInitParameter("config");

二、ServletContext对象

WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

1)ServletContext对象应用1:多个web组件之间使用它实现数据共享

ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

在serlvet中,可以使用如下语句来设置数据共享

 ServletContext context = this.getServletContext();  //servletContext域对象
context.setAttribute("data", "共享数据"); //向域中存了一个data属性

在另一个servlet中,可以使用如下语句来获取域中的data属性

 ServletContext context = this.getServletContext();
String value = (String) context.getAttribute("data"); //获取域中的data属性
System.out.println(value);

2)通过servletContext对象获取到整个web应用的配置信息

 String url = this.getServletContext().getInitParameter("url");
String username = this.getServletContext().getInitParameter("username");
String password = this.getServletContext().getInitParameter("password");

3)通过servletContext对象实现servlet转发

由于servlet中的java数据不易设置样式,所以serlvet可以将java数据转发到JSP页面中进行处理

 this.getServletContext().setAttribute("data","serlvet数据转发");
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/viewdata.jsp");
rd.forward(request, response);

4)通过servletContext对象读取资源文件

在实际开发中,用作资源文件的文件类型,通常是:xml、properties,而读取xml文件必然要进行xml文档的解析,所以以下例子只对properties文件进行读取(在一个web工程中,只要涉及到写地址,建议最好以/开头)

在web工程中,我们一般来说,是不能采用传统方式读取配置文件的,因为相对的是jvm的启动目录(tomcat的bin目录),所以我们要使用web绝对目录来获取配置文件的地址

读取资源文件的三种方式:

第一种:使用ServletContext的getResourceAsStream方法:返回资源文件的读取字节流

 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");

第二种:使用ServletContext的getRealPath方法,获得文件的完整绝对路径path,再使用字节流读取path下的文件

 String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
String filename = path.substring(path.lastIndexOf("\\")+1);
//相比第一种方法的好处是:除了可以获取数据,还可以获取资源文件的名称
FileInputStream in = new FileInputStream(path);
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");

第三种:使用ServletContext的getResource方法,获得一个url对象,调用该类的openStream方法返回一个字节流,读取数据

 URL url = this.getServletContext().getResource("/WEB-INF/classes/db.properties");
InputStream in = url.openStream();
Properties prop = new Properties();
prop.load(in);
String url1 = prop.getProperty("url");

5)web工程中,不同位置的资源文件的读取方式

一、当资源文件在包下面时

 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
System.out.println(in);

二、资源文件在web-inf下

 in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
System.out.println(in);

三、资源文件在web工程中

 in = this.getServletContext().getResourceAsStream("/db.properties");
System.out.println(in);

6)在非servlet程序中如何读取配置文件:用类装载器

1)用类装载方式读取

 in = StudentDao.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties");

2)用类装载方式读取,把资源当作url对待

 URL url = StudentDao.class.getClassLoader().getResource("db.properties");

这样可以获得资源文件名称:String path = url.getPath();

3)注意:在线程休眠过程中,即使改动了资源文件,获取到的还是原始内容

解决方案:

 URL url = StudentDao.class.getClassLoader().getResource("db.properties");
String path = url.getPath();
FileInputStream in = new FileInputStream(path);
Properties prop = new Properties();
prop.load(in);
System.out.println(prop.getProperty("url"));
try
{
Thread.sleep(1000*15);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
in = new FileInputStream(path);
prop = new Properties();
prop.load(in);
System.out.println(prop.getProperty("url"));

4)注意:用类装载器读取资源文件时,千万要注意,资源文件绝对不能太大,否则极易导致内存溢出

ServletConfig与ServletContext的更多相关文章

  1. ServletConfig和ServletContext

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

  2. JavaEE:Servlet简介及ServletConfig、ServletContext

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

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

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

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

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

  5. ServletConfig、ServletContext 的应用

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

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

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

  7. ServletConfig和ServletContext 区别

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

  8. 谈谈 ServletConfig 和 ServletContext

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

  9. Servlet技术之——概述、实现、细节、获取资源、ServletConfig、ServletContext

    Servlet概述.实现.细节.获取资源.ServletConfig.ServletContext (一) Setvlet基本概述 (1) 什么是Servlet ? Servlet(Server Ap ...

随机推荐

  1. su root 和su - root 的区别

    su - root  is   the same as su - just like login as root, then the shell is login shell,which mean i ...

  2. POJ 3744 Scout YYF I

    分段的概率DP+矩阵快速幂                        Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  3. LR常用函数整理

    1,变量转参数lr_save_string("aaa","param"):将字符串"aaa"或者一个字符串变量,转变成LR的参数{param ...

  4. JVM内存模型和关键参数设置

    一. JVM内存模型: Jvm内存模型是学好Java很重要的一部分,该部分学习能让我们在系统运维的时候,或者优化服务器的时候能够有方法,懂原理. 二. Jvm关键参数: 1. 堆大小设置参数: -Xm ...

  5. FZU2127

    FZU2127:养鸡场 Problem Description Jason买来了n米长的竹篱笆,打算将n米长的竹篱笆所实用来围成一个三角形的养鸡场.为方便起见.养鸡场三条边的长度都为正整数. 同一时候 ...

  6. 推荐ubuntu下的画图工具

    今天发现ubuntu下面也有类似于windows画图的画图工具,功能也比较强大,支持各种格式的图片,也有各种工具,非常方便,安装的方法是: sudo apt-get install kolourpai ...

  7. CentOS6系升级Python2.7版本

    安装前准备 本实例以CentOS6.7为例 [root@E tools]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@E tools]# uname -m x ...

  8. GR32 TImage32的图层绘制原理

    转载:http://blog.sina.com.cn/s/blog_491aced20100ded4.html TImage32的继承顺序如下:TCustomControl->TCustomPa ...

  9. BestCoder Round #86 解题报告

    A.Price List Sol 求和查询 Code #include<cstdio> #include<algorithm> #include<iostream> ...

  10. Python super继承详解

    MRO(Method resolution order)是python用来解析方法调用顺序的,mro中记录了一个类的所有基类的类类型序列,super不是简单地调用基类的方法,而是按照MRO中的顺序来调 ...