JavaWeb ----- ServletContext类

简介

(一个工程只有一个!!!)

  • 1、ServletContext 是一个接口,它表示 Servlet 上下文对象
  • 2、一个 web 工程,只有一个 ServletContext 对象实例。
  • 3、ServletContext 对象是一个域对象。
  • 4、ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候销毁。

域对象

域对象,是可以像 Map 一样存取数据的对象,叫域对象。
这里的域指的是存取数据的操作范围,整个 web 工程。
域对象
存数据:setAttribute()
取数据:getAttribute()
删除数据:removeAttribute()

ServletContext 类的四个作用

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

简单使用(前三个演示)

在web.xml中的配置

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

在Servlet中

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1、获取 web.xml 中配置的上下文参数 context-param
//        getServletConfig().getServletContext();等价于getServletContext();
        ServletContext servletContext = getServletConfig().getServletContext();
        String username = servletContext.getInitParameter("username");
        String password = servletContext.getInitParameter("password");
        System.out.println(username+"and"+password);
//        2、获取当前的工程路径,格式: /工程路径
        System.out.println("当前工程路径"+servletContext.getContextPath());
//        3、获取工程部署后在服务器硬盘上的绝对路径
        System.out.println("当前工程部署路径是"+servletContext.getRealPath("/"));
        System.out.println("当前工程css路径是"+servletContext.getRealPath("/css"));
    }
}

第四个功能的演示

写两个Servlet

第一个

package com.stackery.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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 {
        ServletContext servletContext = getServletContext();
        servletContext.setAttribute("user","y4tacker");
        System.out.println("contextServlet1中获取的user值是"+servletContext.getAttribute("user"));

    }
}

第二个

package com.stackery.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        System.out.println("contextServlet2中获取的user值是"+servletContext.getAttribute("user"));
    }
}

猜你喜欢

转载自blog.csdn.net/solitudi/article/details/107229239