servlet ServletConfig ServletContext
ServletConfig对象
在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数。
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中国,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
配置文件web.xml中某个<servlet></servlet>中间有这么一段:
<init-param>
<param-name>data</param-name>
<parm-value>xxxxx</param-value>
<init-param>
<!--<load-on-startup>2</load-on-startup>-->
那么该servlet实例化的时候会把这些内容一起加载。
servlet中的代码(复写了init):
……entends HttpServlet{
private ServletConfig config;
……doGet……
String value = config.getInitParameter("data");//得到xxxxx
……
……doPost……
public void init(ServletConfig config)throws ServletExceptio{
//super.init(config);
this.config = config;
}
其实父类HttpServlet的父类GenericSrvlet中已经定义了这么个私有的东西,并在其init方法中获取了。还提供了一个获取方法:
public ServletConfig getServletConfig(){
return config;
}
我们可以在自己的类中直接用这个方法获得。
得到所有:
Enumeration e = this.getServletConfig().getInitParameterNames();
whie(e.hasMoreElements()){
String name = (String)e.nexElement();
String value = this.getServletConfig().getInitParameter(name);
}
用到的场合。
比如:
解码方式可以配置一个
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
ServletContext
web容器在启动时,它会为每个web应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
获取方式:
方式一,
ServletContext context = this.getServletConfig().getServletContext();
方式二,
context = this.getServletContext();
ServletContext的应用:
由于一个web应用中所有的Servlet共享同一个ServletContext对象,所以多个Servlet通过ServletContext对象实现数据共享。
ServletContext对象通常也称之为context域对象。
域:范围。 context域对象:作用范围是整个应用程序的对象。除此之外还有request session page域对象。
一个Servlet1中有如下代码:
String data ="aaa";
this.getServletContext().setAttribute("data",data);
另一个Servlet2中有如下代码:
String value = (String)this.getServletContext().getAttribute("data");
当Servlet1执行过,Servlet2就可以获取aaa。
比如聊天室就可以用这个servltContext。
web.xml中的<web-app></web-app>中间可以有这么一段:
<context-param>
<param-name>data</param-name>
<param-value>xxxx</param-value>
<param-name>data2</param-name>
<param-value>xxxx2</param-value>
</context-param>
加载该web应用就会自动把该参数加载。
在程序中获取:
this.getServletContext().getInitParameterNames();和ServletConfig类似。
在配置文件中配置改起来很方便。如果在程序中改要改代码,重新编译比较麻烦。
ServletContext还实现了Servlet的转发:
String data = "abc";
data = "<meta http-equiv='fresh' content='3;url=/myweb/index.jsp'>3秒后将跳转";
//this.getServletContext().setAttribute("data",data);由于servlet是单例子的,所以这样有可能最后加进去的data是bcd而非abc,所以这种设置全局的方式不要采用
//不过可以在jsp页面通过获取全局某个属性的方式显示这个data:<% String str = (String)Applcation.getAttribute("data");out.write(str); %>
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("1.jsp");
rd.forward(request,response);//页面将转至1.jsp页面,与重定向不同,这样仅请求了一次服务器,而重定向是两次。
Servletcontext读取资源文件.properties文件:
关于properties文件:
properties和xml都是资源文件,不同之处是xml一般用来存有关系的数据,而properties文件则存放类似用户名、密码、数据库连接地址之类没有关系的数据。
properties文件内容样例:
url=jdbc:mysql://localhost:3306/test
username=admin
password=admin
如上面所写要带个等号。
下面写一下
Servletcontext读取资源文件代码示例:
Inputstream in = this.getServletContext().getResourceAdStrem("/db.properties");
关于这个相对路径的书写,用context读取,相对路径中的第一个"/"表示web应用目录。例如,你在开发的时候把properties放在src下,那么部署到服务器后会出现在“根目录/WEB-INF/classes/”下,这个时候应该写“/WEB-INF/classes/xx.properties”。如果直接用读取流,那么相对路径就变成了启动Tomcat服务器的目录,即Tomcat下的bin目录。
Properties props = new Properties();//map
props.load(in);
String url = props.getProperty("url");//获取键值
更多内容参见API
servlet ServletConfig ServletContext的更多相关文章
- Server,Servlet,ServletConfig,ServletContext,Session,Request,Response
Server流程 解析URL->找到应用->找到Servlet->实例化Servlet->调用init->调用service->返回响应->调用destroy ...
- JavaWeb之Servlet: ServletConfig 与 ServletContext
ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...
- Servlet开发(三)之ServletConfig,ServletContext
1. ServletConfig Servlet是开发动态web的技术,而web.xml是Tomcat工程中最基础也最重要的配置文件,Tomcat启动项目的时候会加载并读取这个文件,其中web.xml ...
- JavaWeb -- 服务器传递给Servlet的对象 -- ServletConfig, ServletContext,Request, Response
1. ServletConfig 有一些东西不合适在程序中写死,应该写在web.xml中,比如 文字怎么显示, 访问数据库名 和 密码, servlet要读取的配置文件 等等.. l在Servle ...
- Servlet、ServletContext与ServletConfig的详解及区别
Servlet.ServletContext与ServletConfig的详解及区别 一.Servlet详解 Servlet是一个interface,全局限定名:javax.servlet.Servl ...
- 02 http,servlet,servletconfig,HttpServletRequest ,HttpServletResponse
Http协议 协议:双方在交互.通讯的时候, 遵守的一种规范.规则.http协议:针对网络上的客户端 与 服务器端在执行http请求的时候,遵守的一种规范. 其实就是规定了客户端在访问服务器端的时候, ...
- 第一个web程序(web.xml , ServletConfig , ServletContext)
一:第一个jsp程序 1.项目设计结构 2.新建Person.java package com.java.demo; public class Person { public void printSt ...
- Servlet之ServletContext以及文件操作
ServletContext ------------------------------------ ServletContext是什么? 与cookie,session比较. 可以把它想象成一个共 ...
- 2.深入学习Servlet的ServletContext对象
一.建立项目servlet01 在入门Servlet项目中建立一个子项目模块(此处不再赘述如何建立),补全maven项目中的java和resources文件夹,添加类HelloServlet.java ...
随机推荐
- arcgis10.5新功能图形缓冲
摘要 在输入要素周围某一指定距离内创建缓冲区多边形.在要素周围生成缓冲区时,大部分制图形状对缓冲区末端(端头)和拐角(连接)可用. 插图
- Quartz调用大全
Quartz调用大全 1.Quartz应用范围广泛,可单独执行也可在spring中嵌入执行. 类似的定时任务在linux下可以用crontab执行 2.实现代码: QuartzTest :主要执行类 ...
- Tomcat6内存不足问题及解决方法
1.Tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,有可能导致系统无法运行.常见的问题是报Tomcat内存溢出错误,Out of Memory(系统内存不足)的异常 ...
- json和gson的区别
json是一种数据格式,便于数据传输.存储.交换gson是一种组件库,可以把java对象数据转换成json数据格式 GSON简单处理JSON json格式经常需要用到,google提供了一个处理jso ...
- 如何在Jenkins中使用环境变量
以BUILD_NUMBER为例, 1.在windows batch命令中使用此环境变量,使用%BUILD_NUMBER%即可 2.在Linux shell命令中使用此环境变量,使用${BUILD_NU ...
- pythonUnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
今天做网页到了测试和数据库交互的地方,其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是Python的str默认是ascii编码,和uni ...
- [转]聊聊技术选型 - Angular2 vs Vue2
转载:https://juejin.im/post/58cab85b44d9040069f38f7a "Come, and take choice of all my library, An ...
- 第二十三章 springboot + 全局异常处理
一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...
- Git的简单介绍
每次看到别人写Git的文章,同学中也有用Git感觉很高大上的感觉,工作中用的是SVN,周末倒腾了一下Git,Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 与 ...
- Reorder List leetcode java
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...