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对象的更多相关文章

  1. 一、HttpServletRequest接口 二、HttpServletReponse接口 三、POST和GET请求方式及其乱码处理 四、ServletContext对象和ServletConfig对象

    一.HttpServletRequest接口 内部封装了客户端请求的数据信息 接收客户端的请求参数.HTTP请求数据包中配置参数 ###<1>常用方法 getContextPath()重要 ...

  2. Servlet接口的实现类,路径配置映射,ServletConfig对象,ServletContext对象及web工程中文件的读取

    一,Servlet接口实现类:sun公司为Servlet接口定义了两个默认的实现类,分别为:GenericServlet和HttpServlet. HttpServlet:指能够处理HTTP请求的se ...

  3. servlet中的ServletConfig对象

    ServletConfig对象对应web.xml文件中的<servlet>节点.当Tomcat初始化一个Servlet时,会创建ServletConfig对象,并将该Servlet的配置信 ...

  4. Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象

    本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...

  5. HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象

    1 HTTP协议特点   1)客户端->服务端(请求request)有三部份     a)请求行--请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号 请求行中的GET ...

  6. 小谈-—ServletConfig对象和servletContext对象

    一.servletContext概述 servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者 ...

  7. Servlet 使用ServletConfig对象来配置Servlet

    ServletContext和ServletConfig的关联 相同点: 1.都可以用来配置Servlet 2.都可以写在web.xml中. 区别点: 1.ServletContext对象,对于所有的 ...

  8. ServletConfig对象和ServletContext对象

    (1)ServletConfig:用来保存一个Servlet的配置信息的(比如 : name, class, url ... ) 这些配置信息没什么大用处,我们还可以在ServletConfig中保存 ...

  9. [原创]java WEB学习笔记05:Servlet中的ServletConfig对象

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

随机推荐

  1. 解决Parameter '__frch_item_0' not found. Available parameters 问题

    1. 问题描述: 出现如下问题,执行报错信息 Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org ...

  2. c# 获取端口的连接数,网站的连接数

    端口连接数: public static int PortTcpConnection(int port) { IPGlobalProperties properti = IPGlobalPropert ...

  3. 如何将JPG格式的图片转换成PNG格式

    study from : https://jingyan.baidu.com/article/6079ad0e63a4fc28ff86db37.html

  4. Python操作文件-20181121

    Python操作文件 Python操作文件和其他语言一样,操作的过程无非是先定位找到文件.打开文件,然后对文件进行操作,操作完成后关闭文件即可. 文件操作方式:对文件进行操作,主要就是读.写的方式,p ...

  5. solr面板的使用

    创建数据库 先别着急点击Add Core,先去目录下创建几个文件. 去solrhome目录下创建一个文件夹,比如test,这个文件夹就是数据库文件夹.

  6. LFYZ-OJ ID: 1016 输油管道问题

    分析 根据之前的证明,我们已经知道最佳输油管线的y位置就是所有油井y坐标序列的中位数,故解题过程为: 1. 读入n个y数据 2. 对n个y数据进行排序(升序或降序) 3. 求中位数zws 4. 计算输 ...

  7. vue 开发微信小程序

    介绍 mpvue (github 地址请参见)是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js的 runtime 和 compiler 实 ...

  8. Geometric regularity criterion for NSE: the cross product of velocity and vorticity 3: $u\times \f{\om}{|\om|}\cdot \f{\vLm^\be u}{|\vLm^\be u|}$

    在 [Chae, Dongho; Lee, Jihoon. On the geometric regularity conditions for the 3D Navier-Stokes equati ...

  9. INI配置文件的格式

    为什么要用INI文件?如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序出厂后还能根据需要进行必要 ...

  10. django上下文处理器的基本使用

    1.定义一个方法 2.在django里面的settings.py里面修改配置文件 3.最后在模板里面调用 操做步骤如下: 这是在settings.py里面配置的文件   在模板里面调用上下文处理器