Servlet(八):ServletContext对象和ServletConfig对象
ServletContext 对象:
问题:
Request 解决了一次请求内的数据共享问题,session 解决了用户不同请求的数据共享问题,那么不同的用户的数据共享该怎么办呢?
解决:
使用 ServletContext 对象
作用:
解决了不同用户的数据共享问题
原理:
ServletContext 对象由服务器进行创建,一个项目只有一个对象。不管在项目的任意位置进行获取得到的都是同一个对象,那么不同用户发起的请求获取到的也就是同一个对象了,该对象由用户共同拥有。
特点:
服务器进行创建
用户共享
一个项目只有一个
生命周期:
服务器启动到服务器关闭
作用域:
项目内
使用:
获取 ServletContext 对象
使用作用域进行共享数据流转
获取 web.xml 中的全局配置
获取 webroot 下项目资源流对象
获取 webroot 下资源绝对路径
案例:网页浏览器次数统计,详见源码
/**
* ServletContext对象学习:
* 问题:
* 不同的用户使用相同的数据
* 解决:
* ServletContext对象
* 特点:
* 服务器创建
* 用户共享
* 作用域:
* 整个项目内
* 生命周期:
* 服务器启动到服务器关闭
* 使用:
* 获取ServletContext对象
* //第一种方式:
ServletContext sc=this.getServletContext();
//第二种方式:
ServletContext sc2=this.getServletConfig().getServletContext();
//第三种方式:
ServletContext sc3=req.getSession().getServletContext();
* 使用ServletContext对象完成数据共享
* //数据存储
* sc.setAttribute(String name, Object value);
* //数据获取
* sc.getAttribute("str") 返回的是Object类型
* 注意:
* 不同的用户可以给ServletContext对象进行数据的存取。
* 获取的数据不存在返回null。
* 获取项目中web.xml文件中的全局配置数据
* sc.getInitParameter(String name); 根据键的名字返回web.xml中配置的全局数据的值,返回String类型。
* 如果数据不存在返回null。
* sc.getInitParameterNames();返回键名的枚举
* 配置方式:注意 一组<context-param>标签只能存储一组键值对数据,多组可以声明多个 <context-param>进行存储。
* <context-param>
<param-name>name</param-name>
<param-value>zhangsan</param-value>
</context-param>
作用:将静态数据和代码进行解耦。
获取项目webroot下的资源的绝对路径。
String path=sc.getRealPath(String path);
获取的路径为项目根目录,path参数为项目根目录中的路径
获取webroot下的资源的流对象
InputStream is = sc.getResourceAsStream(String path);
注意:
此种方式只能获取项目根目录下的资源流对象,class文件的流对象需要使用类加载器获取。
path参数为项目根目录中的路径
*
*
* @author MyPC
*
*/
public class ServletContextServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取ServletContext对象
//第一种方式:
ServletContext sc=this.getServletContext();
//第二种方式:
ServletContext sc2=this.getServletConfig().getServletContext();
//第三种方式:
ServletContext sc3=req.getSession().getServletContext();
System.out.println(sc==sc2);
System.out.println(sc==sc3);
//使用ServletContext对象完成数据共享
//数据存储
sc.setAttribute("str", "ServletContext对象学习");
//获取项目web.xml的全局配置数据
String str = sc.getInitParameter("name2");
System.out.println("全局配置参数:"+str);
//获取项目根目录下的资源的绝对路径
//String path="D:\\apache-tomcat-7.0.56\\webapps\\sc\\doc\\1.txt";
String path=sc.getRealPath("/doc/1.txt");
System.out.println(path);
//获取项目根目录下资源的流对象
InputStream is = sc.getResourceAsStream("/doc/1.txt"); }
}
public class ServletContextServlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//创建ServletContext对象
ServletContext sc=this.getServletContext();
//获取数据
System.out.println("ServletContextServlet2.service()"+sc.getAttribute("str"));
}
}
ServletConfig 对象:
问题:
使用ServletContext对象可以获取web.xml中的全局配置文件,在 web.xml 中每个 Servlet 也可以进行单独的配置,那么该怎么获取配置信息呢?
解决:
使用 ServletConfig 对象
作用:
ServletConfig 对象是 Servlet 的专属配置对象,每个 Servlet 都单独拥有一个 ServletConfig 对象,用来获取 web.xml 中的配置信息。
使用:
获取 ServletConfig 对象
获取 web.xml 中 servlet 的配置信息
/**
* ServletConfig对象学习:
* 问题:
* 如何获取在web.xml中给每个servlet单独配置的数据呢?
* 解决:
* 使用ServletConfig对象
* 使用:
* 获取ServletConfig对象
* 获取web.xml中的配置数据
* @author MyPC
*
*/
public class ServletConfigServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取ServletConfig对象
ServletConfig sc=this.getServletConfig();
//获取web.xml中的配置数据
String code=sc.getInitParameter("config");
System.out.println(code);
}
}
Servlet(八):ServletContext对象和ServletConfig对象的更多相关文章
- 一、HttpServletRequest接口 二、HttpServletReponse接口 三、POST和GET请求方式及其乱码处理 四、ServletContext对象和ServletConfig对象
一.HttpServletRequest接口 内部封装了客户端请求的数据信息 接收客户端的请求参数.HTTP请求数据包中配置参数 ###<1>常用方法 getContextPath()重要 ...
- Servlet接口的实现类,路径配置映射,ServletConfig对象,ServletContext对象及web工程中文件的读取
一,Servlet接口实现类:sun公司为Servlet接口定义了两个默认的实现类,分别为:GenericServlet和HttpServlet. HttpServlet:指能够处理HTTP请求的se ...
- servlet中的ServletConfig对象
ServletConfig对象对应web.xml文件中的<servlet>节点.当Tomcat初始化一个Servlet时,会创建ServletConfig对象,并将该Servlet的配置信 ...
- Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象
本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...
- HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象
1 HTTP协议特点 1)客户端->服务端(请求request)有三部份 a)请求行--请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号 请求行中的GET ...
- 小谈-—ServletConfig对象和servletContext对象
一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...
- Servlet 使用ServletConfig对象来配置Servlet
ServletContext和ServletConfig的关联 相同点: 1.都可以用来配置Servlet 2.都可以写在web.xml中. 区别点: 1.ServletContext对象,对于所有的 ...
- ServletConfig对象和ServletContext对象
(1)ServletConfig:用来保存一个Servlet的配置信息的(比如 : name, class, url ... ) 这些配置信息没什么大用处,我们还可以在ServletConfig中保存 ...
- [原创]java WEB学习笔记05:Servlet中的ServletConfig对象
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
随机推荐
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...
- vscode设置python3.7调试环境(已更新)
汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai CentOS安装Python3.7:https://www.cnblogs.com/do ...
- c#操作IIS之IISHelper
//----------------------------------------------------------------------- // <copyright file=&quo ...
- jenkins系列之插件配置(二)
第一步:下面来安装nodejs插件 第二步:可以看到,Jenkins提供了丰富的插件供开发者使用,找到需要的[NodeJS Plugin],勾选后点击安装即可 我的是已经安装了 第三步: 安装完毕后, ...
- 微服务之服务中心—zookeeper
微服务中的服务注册与发现 传统的项目中,某个服务访问另一个服务,可以通过在配置文件中记录其他服务静态地址的形式进行访问,通常这个配置文件也很少更新,模式如下图: 而在微服务中,每个功能可能都是一个独立 ...
- HMM隐马尔科夫算法(Hidden Markov Algorithm)初探
1. HMM背景 0x1:概率模型 - 用概率分布的方式抽象事物的规律 机器学习最重要的任务,是根据一些已观察到的证据(例如训练样本)来对感兴趣的未知变量(例如类别标记)进行估计和推测. 概率模型(p ...
- .net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密
写项目时,后台一直用md5加密,一天群里人问,除了MD5还有其它的加密方法吗?当时只知道还有个SHA,但怎么实现什么的都不清楚,于是当网上找了下,把几种常见的加密方法都整理了下,用winform写了个 ...
- Linux Shell远程执行命令
1.问题描述 经常需要远程到其他节点上执行一些shell命令,如果分别ssh到每台主机上再去执行很麻烦,下边介绍shell命令远程执行的方法. 前提: 远程电脑之间已经配置ssh免密码登陆 2.脚本方 ...
- maven 分隔环境
在pom.xml 上 添加 把要分隔的环境 文件 弄成这样 打包 mvn clean package -Dmaven.test.skip=true -P+环境名 例子:mvn clean packag ...
- DCM、PLL、PMCD、MMCM相关
摘自网上 : http://xilinx.eetop.cn/viewnews-1482 The DCM is a Digital Clock Manager - at its heart it is ...