mvc-servlet---ServletConfig与ServletContext对象详解(转载)
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>中的配置信息了
//获取初始化参数
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)注意:用类装载器读取资源文件时,千万要注意,资源文件绝对不能太大,否则极易导致内存溢出
mvc-servlet---ServletConfig与ServletContext对象详解(转载)的更多相关文章
- ServletConfig与ServletContext对象详解
一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数.(配置在某个servlet标签或者整个w ...
- jQuery的deferred对象详解(转载)
本文转载自: jQuery的deferred对象详解(转载)
- django中request对象详解(转载)
django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将 HttpRequest对象 作为第一个参数传入该函数. ...
- Servlet 3.0 新特性详解 (转载)
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Jav ...
- 火狐和IE的window.event对象详解(转载)
FF的FIREBUG,不仅能测试JS还能检查CSS错误,是一般常用的. 但它主要检查FF方面的错误,对IE就无能为力了. 要测试IE,就用ieTester,它可以测试IE几乎所有版本(1.0恐怕也用不 ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- JavaWeb学习----JSP内置对象详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- ServletConfig与ServletContext对象(接口)
ServletConfig:封装servlet的配置信息. 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数. <ser ...
- Servlet技术——request、respone详解
Servlet之request.respone详解 Request (一) 概述 request是Servlet.service()方法的一个参数,在客户端发出每个请求时,服务器都会创建一个reque ...
随机推荐
- iOS开发--应用设置及用户默认设置【1、bundle的运用】
在iphone里面,应用都会在“设置”里面有个专属的应用设置,选择该菜单界面,用户便可以在其中输入和更改各种选项,协助用户更便捷设置个人喜好与习惯. 在这一节中,希望能通过对捆绑包(bu ...
- Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath
启动openfire后出现这个错误,貌似什么配置没对吧.网上搜索了下,找到解决办法, $ vi /etc/profile在里面加入:export openfireHome=/opt/openfire ...
- margin:0 auto在IE中失效的解决方案
转自:http://www.cnblogs.com/hongchenok/archive/2012/11/29/2795041.html 最近在开发项目的时候,发现在火狐浏览器中设置外容器margin ...
- 1. IOS 9.3.3描述文件没了处理方法
1.用手机登录:https://beta.apple.com/ 2.找到"注册您的设备" 3.往下拉第二点就是,直接点击下载即可.
- 聊聊javascript中的面向对象
面向对象这个东西一直晕晕乎乎的,正好这段时间没有活,可以好好整理整理了! 1.什么是对象? 其实这个说起来一切东西都是对象 2.目前我们使用对象的时候,使用的是两种设计模式杂糅起来的 分别是原型模式和 ...
- javascript 原型详解
引:http://www.cnblogs.com/wangfupeng1988/p/3978131.html 1.什么是javascript原型 每一个函数都有prototype属性(默认生成的)和原 ...
- Jquery Mobile 动态添加元素然后刷新 转
Jquery Mobile 动态添加元素然后刷新 (2013-05-09 12:39:05) 转载▼ 标签: it 分类: Mobile jquery 表单元素每一个元素都有自己刷新的方法,每当改变它 ...
- clang format 官方文档自定义参数介绍(中英文)
官方文档:http://clang.llvm.org/docs/ClangFormatStyleOptions.html 中文 在代码中配置样式 当使用 clang::format::reformat ...
- Android 操作SQLite基本用法
一.SQLite的介绍 1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的 ...
- HTTP-崔希凡笔记
HTTP协议(重点) 协议:协议的甲乙双方,就是客户端(浏览器)和服务器! 理解成双方通信的格式! l 请求协议: l 响应协议: 1 安装HttpWatch HttpWatch是专门为IE浏览器 ...