ServletContext的使用

28 篇文章 0 订阅

一、什么是 ServletContext

  • ServletContext 是一个接口,它表示 Servlet 上下文对象
  • 一个 web 工程,只有一个 ServletContext 对象实例。
  • ServletContext 对象是一个域对象。域对象,是可以像 Map 一样存取数据的对象,叫域对象。 这里的指的是存取数据操作范围,整个 web 工程。
  • ServletContext 是在 web 工程部署启动的时候创建。在 web工程停止的时候销毁。

二、ServletContext 类的四个作用

  • 获取 web.xml 中配置的上下文参数 context-param
  • 获取当前的工程路径,格式: /工程路径
  • 获取工程部署后在服务器硬盘上的绝对路径
  • Map 一样存取数据,setAttribute()getAttribute()removeAttribute()

web.xml 中的配置

    <!--context-param 是上下文参数(它属于整个 web 工程)-->
    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>123456</param-value>
    </context-param>

	<servlet>
        <servlet-name>ContextServlet</servlet-name>
        <servlet-class>Servlet.ContextServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ContextServlet</servlet-name>
        <url-pattern>/contextServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ContextServlet1</servlet-name>
        <servlet-class>Servlet.ContextServlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ContextServlet1</servlet-name>
        <url-pattern>/contextServlet1</url-pattern>
    </servlet-mapping>
public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1、获取 web.xml 中配置的上下文参数 context-param
        ServletContext servletContext = getServletConfig().getServletContext();
        String username = servletContext.getInitParameter("username");
        String password = servletContext.getInitParameter("password");
        System.out.println("context-param 参数 username 的值是:" + username);
        System.out.println("context-param 参数 password 的值是:" + password);

//        2、获取当前的工程路径,格式: /工程路径
        System.out.println( "当前工程路径:" + servletContext.getContextPath() );

//        3、获取工程部署后在服务器硬盘上的绝对路径
            /*
                / 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到 IDEA 代码的 web 目录<br/>
            */
        System.out.println("工程部署的路径是:" + servletContext.getRealPath("/"));
    }
}

public class ContextServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        4、像 Map 一样存取数据
        // 获取 ServletContext 对象
        ServletContext context = getServletContext();
        System.out.println(context);    //整个web工程只有一个context

        System.out.println("保存之前: Context1 获取 key1 的值是:"+ context.getAttribute("key1"));

        context.setAttribute("key1", "value1");

        System.out.println("Context1 中获取域数据 key1 的值是:"+ context.getAttribute("key1"));
    }
}

输出结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletContext是Java Web的一个重要接口,它代表了Web应用程序在服务器的上下文环境。在一个Web应用程序,每个Servlet都可以访问同一个ServletContext对象,从而实现Servlet之间的数据共享和通信。 在Java,可以使用ServletConfig对象的getServletContext()方法来获取ServletContext对象,然后使用该对象的方法来实现一系列操作,例如: 1. 获取Web应用程序的初始化参数:可以使用ServletContext对象的getInitParameter()方法来获取Web应用程序的初始化参数,例如数据库连接等配置信息。 ```java String username = context.getInitParameter("username"); ``` 2. 获取Web应用程序的真实路径:可以使用ServletContext对象的getRealPath()方法来获取Web应用程序的真实路径,例如获取Web应用程序的一个文件的绝对路径。 ```java String path = context.getRealPath("/WEB-INF/config.properties"); ``` 3. 在ServletContext保存数据:可以使用ServletContext对象的setAttribute()方法来在ServletContext保存数据,从而实现Servlet之间的数据共享。 ```java context.setAttribute("key", value); ``` 4. 从ServletContext获取数据:可以使用ServletContext对象的getAttribute()方法来从ServletContext获取数据,例如获取其他Servlet保存的数据。 ```java Object value = context.getAttribute("key"); ``` 需要注意的是,ServletContext对象的作用域为整个Web应用程序,因此需要注意数据的安全性和可靠性。另外,ServletContext对象是在Web应用程序启动时创建的,因此可以在Servlet的init()方法获取ServletContext对象并进行初始化操作,例如读取配置文件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值