Servlet 使用ServletContext共享数据,读取web.xml配置
ServletContext对象
session和cookie,对于每一个请求用户来说,都是不同的,因为要保证隐私安全。
而有一些数据,可以让所有用户共享,此时就可以用ServletContext对象。只要在一个Servlet中设置了ServletContext属性值,那么本项目的中任意一个Servlet中都可以获取到设置的属性值。作用范围是所有Servlet,而有效期是服务器关闭之前。
public class TestServletContext extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取Servlet的三种方式,获取的ServletContext对象是相同的。
//ServletContext context = this.getServletContext();
//context = req.getSession().getServletContext();
//ontext = this.getServletConfig().getServletContext();
ServletContext context = this.getServletContext();
// 设置属性值setAttribute(String name, Object object)
context.setAttribute("name", "张三");
// 获取属性值,如果不存在,则返回null
String name = (String)context.getAttribute("name");
}
}
使用ServletContext来获取web.xml中的配置项
之前讲了可以使用Properties工具类来获取配置文件中的配置项,这里,同样可以使用ServletContext来实现。
不过需要注意的是,ServletContext对象能够读取的配置项需要专门配置:
# 下面创建了两个配置项,分别是name=张三,age=99
<context-param>
<param-name>name</param-name>
<param-value>张三</param-value>
</context-param> <context-param>
<param-name>age</param-name>
<param-value>99</param-value>
</context-param>
使用ServletContext读取配置的实例:
ServletContext context = this.getServletContext();
String name = (String)context.getInitParameter("name");
int age = Integer.parseInt(context.getInitParameter("age"));
System.out.println("从web.xml读取到全局配置如下:\n" + "name :" + name + "\t age :" + age);
运行结果如下:
从web.xml读取到全局配置如下:
name :张三 age :99
从其他文件中读取数据
读取数据的时候,有一个问题就是路径的问题,如果使用绝对路径,一旦服务器代码改变位置,路径就会错误,一般都会使用相对路径。
相对路径有时候并不能准确的表示文件的位置,即使能都找到,在定位文件的时候也会有点麻烦,
可以在指定路径的时候,以项目根路径为准,来定位文件。
// 获取以项目webRoot目录为根目录,给定path的资源文件,返回绝对路径
String path = context.getRealPath("/doc/intro.txt");
System.out.println(path);
// E:\javaee\.metadata\.me_tcat85\webapps\MySite\doc\intro.txt // 获取webRoot下的资源流对象
InputStream config = context.getResourceAsStream("/doc/intro.txt");
// 实际是读webRoot/doc/intro.txt byte[] b = new byte[1000];
int length = config.read(b);
System.out.println(new String(b, 0, length));
// 打印配置文件中的数据
Servlet 使用ServletContext共享数据,读取web.xml配置的更多相关文章
- servlet中用注解的方式读取web.xml中的配置信息
在学习servletContext的时候,我们知道了可以在web.xml中通过<context-param>标签来定义全局的属性(所有servlet都能读取的信息),并在servlet中通 ...
- web.xml配置以及一些详解
web.xml的根元素定义如下所示(代表当前使用哪个模版): <?xml version="1.0" encoding="UTF-8"?> < ...
- web.xml配置重理解
<context-param> <param-name>home-page</param-name> <param-value>home.jsp< ...
- web.xml配置之<context-param>详解
读完本文,你将了解(1)<context-param>的作用和用法,(2)<context-param>配置和<init-param>的区别 <context ...
- springmvc学习指南 之---第27篇 spring如何实现servlet3.0无web.xml 配置servlet对象的
writedby 张艳涛 基于web.xml配置,有人说麻烦,tomcat给按照servlet3.0,实现了基于注解@WebServlet,有人说springmvc的springmvc.xml配置麻烦 ...
- springMVC WebApplicationInitializer 替代web.xml 配置Servlet 之原理
Servlet 3.0之前 ,xml 配置 在过去搭建spring + springMCV ,首先第一步要做的是什么 ,就是要配置web.xml 文件 ,把springMVC 中的Servlet 加 ...
- 初学servlet之使用web.xml配置
先写两个servlet,之后展示web.xml配置 package app01c;import java.io.IOException;import java.io.PrintWriter;impor ...
- eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet& ...
- java web.xml配置详解
1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...
随机推荐
- oracle USING 用法
提问 using(xx)中可以接多个列吗? using(xx)中的列可以接表名或别名吗? 在使用using的语句中,select * 可以使用吗? 如果表有别名t,select t.* from ta ...
- Django Form和ModelForm组件
Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...
- 自然语言处理之word2vec
在word2vec出现之前,自然语言处理经常把字词转为one-hot编码类型的词向量,这种方式虽然非常简单易懂,但是数据稀疏性非常高,维度很多,很容易造成维度灾难,尤其是在深度学习中:其次这种词向量中 ...
- 机器学习算法总结(九)——降维(SVD, PCA)
降维是机器学习中很重要的一种思想.在机器学习中经常会碰到一些高维的数据集,而在高维数据情形下会出现数据样本稀疏,距离计算等困难,这类问题是所有机器学习方法共同面临的严重问题,称之为“ 维度灾难 ”.另 ...
- Maven打包相关插件集合
参考:https://www.cnblogs.com/selier/p/9510326.html
- 史上最简单的springboot国际化多语言切换实现方案
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 前提: 在resources目录下建立 messages_en_US.properti ...
- 【vue】vue +element 实现批量删除
相关资料:http://blog.csdn.net/eagle_88/article/details/70238836 作者:smile.轉角 QQ:493177502
- 002_cookie的session_id解释
HTTP协议(http://www.w3.org/Protocols/)是“一次性单向”协议. 服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Reques ...
- Git中删除指定文件
之前的博客Git基础使用教程介绍了Git这个开源分布式管理系统的一些基础操作,这篇博客,介绍下如何利用Git删除远程仓库的文件... 1.拉取远程仓库的文件到本地 git clone xxx 如果还未 ...
- Linux中断管理 (3)workqueue工作队列
目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...