ServletContext上下文对象的特点以及用法

1.概述

Servletcontex上下文对象,当application启动时,Tomcat服务器会根据web.xml以及注解的方式,创建一个唯一的ServletContext对象,包含整个项目中很多核心的资源数据 ,并且是一个整个WEB  Application的【公共区域】,所有的Servlet程序和Jsp程序都可以使用ServletContext中存储的数据。当Application项目关闭时,ServletContext对象被销毁。

2.特点

  1. Servletcontext是整个项目中的一个公共共享资源,所有的Servlet都可以对其进行访问,并且只有一个。
  2. 他是直接作用于Web Application。
  3. 服务器启动时就会加载,服务器关闭时销毁。
  4. ServletContext可以进行数据的共享,一般用来进行Servlet之间的参数的传递。

API

//配置对象可以获取
interface ServletConfig{
    //获取上下文对象
    ServletContext getServletcontext();
}
//请求对象
interface ServletRequest{
    //获取上下文对象
    ServletContext getServletcontext();
}
abstract class GenericServlet {
    ServletContext getServletContext();
}
interface HttpSession {
    ServletContext getServletContext();
}
interface ServletContext {
    // 获取盘符中的真实路径
    String getRealPath(String path);

    // 获取服务器信息
    String getServerInfo();
    
    // 获取当前web项目中的根路径
    String getContextPath();
    
    // 获取属性键值对
    Object getAttribute(String key);
    
	// 设置属性键值对
    void setAttribute(String key, Object value);
    
	// 移除属性键值对
    void removeAttribute(String key);
}

大牛勿喷,刚学俩月。(合十)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值