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 = newProperties();
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 = newFileInputStream(path);
Properties prop = newProperties();
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 = newProperties();
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 = newFileInputStream(path);
Properties prop = newProperties();
prop.load(in);
System.out.println(prop.getProperty("url"));
try {
Thread.sleep(1000*15);
} catch (InterruptedExceptione) {
e.printStackTrace();
}
in = newFileInputStream(path);
prop = new Properties();
prop.load(in);
System.out.println(prop.getProperty("url"));
4)注意:用类装载器读取资源文件时,千万要注意,资源文件绝对不能太大,否则极易导致内存溢出
ServletConfig与ServletContext对象详解的更多相关文章
- mvc-servlet---ServletConfig与ServletContext对象详解(转载)
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
- JavaWeb学习----JSP内置对象详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- jQuery的deferred对象详解
jQuery的deferred对象详解请猛击下面的链接 http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_ ...
- Window 对象详解 转自 http://blog.csdn.net/jcx5083761/article/details/41243697
详解HTML中的window对象和document对象 标签: HTMLwindowdocument 2014-11-18 11:03 5884人阅读 评论(0) 收藏 举报 分类: HTML& ...
- jQuery的deferred对象详解(转载)
本文转载自: jQuery的deferred对象详解(转载)
- JS中的event 对象详解
JS中的event 对象详解 JS的event对象 Event属性和方法:1. type:事件的类型,如onlick中的click:2. srcElement/target:事件源,就是发生事件的 ...
- django中request对象详解(转载)
django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将 HttpRequest对象 作为第一个参数传入该函数. ...
- (转)javascript中event对象详解
原文:http://jiajiale.iteye.com/blog/195906 javascript中event对象详解 博客分类: javaScript JavaScriptCS ...
- dom对象详解--document对象(二)
dom对象详解--style对象 style对象 style对象和document对象下的集合对象styleSheets有关系,styleSheets是文档中所有style对象的集合,这里讲解的 ...
随机推荐
- 进程池、tornado、字体
协程: import grequests from fake_useragent import UserAgent urls=[f'http://bir删d.so/search?page={p ...
- undo空间满的处理方法(含undo的学习与相关解释)
1.查看数据库当前实例使用的是哪个UNDO表空间: show parameter undo_tablespace 2.查看UNDO表空间对应的数据文件和大小 pages col file_name f ...
- 一些有价值的Blog推荐
待看的一些文章 1. 性能调优攻略 http://coolshell.cn/articles/7490.html 2. 内存的存储管理--段式和页式管理的区别 http://blog.sina.com ...
- 剑指offer编程题Java实现——面试题10二进制中1的个数
题目: 请实现一个函数,输入一个整数,输出该整数二进制表示中1的个数.例如,把9表示成二进制是1001,有2位是1,该函数输出2解法:把整数减一和原来的数做与运算,会把该整数二进制表示中的最低位的1变 ...
- 【接口时序】5、QSPI Flash的原理与QSPI时序的Verilog实现
一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 4.Matlab版本:Matlab2014b/ ...
- PHP 生成验证码(+图片没有显示的解决办法)
今天有需要用到验证码,就敲了个,毕竟用途比较广,所以打算把代码留下来,以后肯定用得上的.当然,今天在做的时候也是有一些问题的,分享出来吧,记录自己所犯的错误,避免以后再掉坑里. 先给个效果图(下面的真 ...
- keepalived-1
keepalived所执行的外部脚本命令建议使用绝对路径 vrrp 广播 keepalived的主要功能 1,管理LVS负载均衡软件 2,对LVS集群节点健康检查功能.Healthcheck 3,
- 使用Ansible实现数据中心自动化运维管理
长久以来,IT 运维在企业内部一直是个耗人耗力的事情.随着虚拟化的大量应用.私有云.容器的不断普及,数据中心内部的压力愈发增加.传统的自动化工具,往往是面向于数据中心特定的一类对象,例如操作系统.虚拟 ...
- Redis最新面试题26题(初级、中级Redis面试题)
Redis 1级(入门基础) 1.Redis有哪些数据类型? string,list,set,sorted set(Zset),hash 2.集合和列表有什么区别? 列表是可以从两端推入.推出数据的队 ...
- Tomcat使用IDEA远程Debug调试
Tomcat运行环境:CentOS6.5.Tomcat7.0.IDEA 远程Tomcat设置 1.在tomcat/bin下的catalina.sh上边添加下边的一段设置 CATALINA_OPTS=& ...