博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet--ServletContext详解
阅读量:2442 次
发布时间:2019-05-10

本文共 2532 字,大约阅读时间需要 8 分钟。

什么是ServletContext对象?

ServletContext代表的是一个web应用的环境(上下文)对象,ServletContext对象内部封装的是该web应用的信息。

一 个 w e b 应 用 只 有 一 个 S e r v l e t C o n t e x t 对 象 。 \color{red}{一个web应用只有一个ServletContext对象。} webServletContext

Servlet的生命周期

  • 创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状态))
  • 销毁:web应用被卸载(服务器关闭,移除该web应用)

获得ServletContext对象

第一种:

ServletContext servletContext = config.getServletContext();

第二种(常用):

ServletContext servletContext1 = this.getServletContext();

ServletContext的作用

  1. 获得web应用全局的初始化参数:
    web.xml中配置初始化参数
testStr
helloWorld

通过ServletContext获得初始化参数:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得ServletContext对象 ServletContext servletContext = this.getServletContext(); //获得初始化参数 String testStr = servletContext.getInitParameter("testStr"); System.out.println(testStr); }

通过浏览器访问,控制台输出:

helloWorld
  1. 获得web应用中任何资源的绝对路径(重要):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得ServletContext对象 ServletContext servletContext = this.getServletContext(); //获的资源相对于该web的相对路径 String realPath = servletContext.getRealPath("index.jsp"); System.out.println(realPath); }

通过浏览器访问,控制台输出:

F:\IDEAworkspace\JavaEEDemo\out\artifacts\JavaEEDemo_war_exploded\index.jsp

ServletContext是一个域对象

域对象:

域对象是服务器在内存上创建的存储空间,用于在不同动态资源(servlet)之间传递与共享数据。

域对象通用方法:

方法 描述
setAttribute(String name,Object value); 向域对象中添加数据,添加时以key-value形式添加
getAttribute(String name); 根据指定的key读取域对象中的数据
removeAttribure(String name); 根据指定的key从域对象中删除数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得ServletContext对象 ServletContext servletContext = this.getServletContext(); //向域对象中存入数据 servletContext.setAttribute("str", "helloWorld"); //从域对象中取出数据 String str = (String) servletContext.getAttribute("str"); //从域对象中删除数据 servletContext.removeAttribute("str"); }

ServletContext存储数据的特点:

全局共享,里面的数据所有动态资源都可以写入和读取。

ServletContext获取当前工程名字

核心方法:

getServletContext().getContextPath();

示例:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得工程名字 String contextPath = getServletContext().getContextPath(); System.out.println(contextPath); }

通过浏览器访问,控制台输出:

/JavaEEDemo

转载地址:http://tnsqb.baihongyu.com/

你可能感兴趣的文章
FreeBSD安装文件系统(转)
查看>>
Windows Vista Beta2 中文版优化归类(转)
查看>>
Makefile编写小说(一) (转)
查看>>
第10章 模型管理视图 (转)
查看>>
InsideJVM(3)--Method area(方法区) (转)
查看>>
中文版Windows XP 的新增功能(转)
查看>>
Web Application 開 發 利 器 - WebSnap(三) (转)
查看>>
跟我学 安装Windows Vista Bata2实录(转)
查看>>
Windows Vista IIS 7.0开启方法(转)
查看>>
Windows Vista六大版本详细介绍(转)
查看>>
单一产品不会成功 开源软件开始商业应用(转)
查看>>
RedHat上SSH2的安装和使用(转)
查看>>
安全使用RedHat Linux系统(转)
查看>>
RedHat Enterprise AS4硬盘安装步骤(转)
查看>>
全国第一个高校Linux培训考试中心建立(转)
查看>>
关于Kerberos安装的几个问题(转)
查看>>
Solaris硬盘分区简介(转)
查看>>
gcc编译器小知识FAQ(转)
查看>>
Linux下多线程编程与信号处理易疏忽的一个例子(转)
查看>>
流氓和木马结合 强行关闭你的防火墙(转)
查看>>