JavaWeb篇_12——ServletContext对象

ServletContext对象

ServletContext对象介绍

ServletContext官方叫Servlet上下文。服务器会为每一个Web应用创建一个ServletContext对象。这个对象全局唯一,而且Web应用中的所有Servlet都共享这个对象。所以叫全局应用程序共享对象。

ServletContext对象的作用

  • 相对路径转绝对路径
  • 获取容器的附加信息
  • 读取配置信息
  • 全局容器
相对路径转绝对路径

context.getRealPath("path")

该方法可以将一个相对路径转换为绝对路径,在文件上传与下载时需要用到该方法做路径的转换。

java 复制代码
   	//获取ServletContext对象
       ServletContext servletContext = this.getServletContext();
       //转换路径
       String realPath = servletContext.getRealPath("image.PNG");
       File file=new File(realPath);
获取容器的附加信息

servletContext.getServerInfo()

返回Servlet容器的名称和版本号

servletContext.getMajorVersion()

返回Servlet容器所支持Servlet的主版本号。

servletContext.getMinorVersion()

返回Servlet容器所支持Servlet的副版本号。

java 复制代码
public class GetBaseInfoServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取ServletContext对象
       ServletContext servletContext = this.getServletContext();
       //获取容器的基本信息
       String serverInfo = servletContext.getServerInfo();
       //获取容器所支持的主版本号
       int majorVersion = servletContext.getMajorVersion();
       //获取容器所支持的副版本号
       int minorVersion = servletContext.getMinorVersion();
       //设置响应编码
       resp.setContentType("text/plain;charset=utf-8");
       PrintWriter pw = resp.getWriter();
       pw.println("容器的基本信息:"+serverInfo);
       pw.println("容器所支持Servlet的版本号为:"+majorVersion+"."+minorVersion);
   }
}
xml 复制代码
    <servlet>
        <servlet-name>getBaseInfoServlet</servlet-name>
        <servlet-class>com.java.GetBaseInfoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getBaseInfoServlet</servlet-name>
        <url-pattern>/getBaseInfo.do</url-pattern>
    </servlet-mapping>
获取web.xml文件中的信息
xml 复制代码
<context-param>
  <param-name>key</param-name>
  <param-value>value</param-value>
</context-param>

servletContext.getInitParameter("key")

该方法可以读取web.xml文件中标签中的配置信息。

servletContext.getInitParameterNames()

该方法可以读取web.xml文件中所有param-name标签中的值。

xml 复制代码
    <context-param>
        <param-name>key1</param-name>
        <param-value>java1</param-value>
    </context-param>

    <context-param>
        <param-name>key2</param-name>
        <param-value>java2</param-value>
    </context-param>
java 复制代码
public class ContextReadInfoServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取ServletContext对象
       ServletContext servletContext = this.getServletContext();
       //获取所有的<param-name>的值
       Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
       PrintWriter pw = resp.getWriter();
       while (initParameterNames.hasMoreElements()){
           String name = initParameterNames.nextElement();
           String value = servletContext.getInitParameter(name);
           pw.println("name:"+name+" = value:"+value);
       }
       pw.flush();
       pw.close();
   }
}
xml 复制代码
	<servlet>
        <servlet-name>contextReadInfoServlet</servlet-name>
        <servlet-class>com.java.ContextReadInfoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>contextReadInfoServlet</servlet-name>
        <url-pattern>/readInfo.do</url-pattern>
    </servlet-mapping>
    
全局容器

servletContext.setAttribute("key",ObjectValue)

向全局容器中存放数据。

servletContext.getAttribute("key")

从全局容器中获取数据。

servletContext.removeAttribute("key")

根据key删除全局容器中的value。

java 复制代码
public class GlobalContainerServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req,resp);
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取ServletContext对象
       ServletContext servletContext = this.getServletContext();
       String s1 = (String) servletContext.getAttribute("key1");
       String s2 = (String)servletContext.getAttribute("key2");
       PrintWriter pw=resp.getWriter();
       pw.println(s1);
       pw.println(s2);
       pw.flush();
       pw.close();
   }

   @Override
   public void init() throws ServletException {
       //获取ServletContext对象
       ServletContext servletContext = this.getServletContext();
       servletContext.setAttribute("key1","java1");
       servletContext.setAttribute("key2","java2");
   }
}

ServletContext对象生命周期

​ 当容器启动时会创建ServletContext对象并一直缓存该对象,直到容器关闭后该对象生命周期结束。ServletContext对象的生命周期非常长,所以在使用全局容器时不建议存放业务数据。

相关推荐
IDOlaoluo31 分钟前
FindBugs-IDEA-1.0.1.zip安装使用教程(IntelliJ IDEA插件手动安装查Bug)
java·bug·intellij-idea
程序员小凯6 小时前
Spring Boot文件处理与存储详解
java·spring boot·后端
Miraitowa_cheems7 小时前
LeetCode算法日记 - Day 88: 环绕字符串中唯一的子字符串
java·数据结构·算法·leetcode·深度优先·动态规划
黑云压城After7 小时前
vue2实现图片自定义裁剪功能(uniapp)
java·前端·javascript
せいしゅん青春之我8 小时前
【JavaEE初阶】网络原理——TCP报文结构、确认应答机制
网络·笔记·网络协议·tcp/ip·java-ee
zcl_19919 小时前
记一次ThreadLocal导致的生产事故
java
RoboWizard9 小时前
怎么判断我的电脑是否支持PCIe 5.0 SSD?Kingston FURY Renegade G5
java·spring·智能手机·电脑·金士顿
毕设源码-钟学长9 小时前
【开题答辩全过程】以 儿童游泳预约系统为例,包含答辩的问题和答案
java·eclipse
皮皮林55110 小时前
5种接口频率监控方案实战对比,性能、成本、复杂度全解析!
java
似水流年 光阴已逝10 小时前
从Jar包到K8s上线:全流程拆解+高可用实战
java·kubernetes·jar