概念

  • ServletContext对象范围:所有用户所有请求的数据

  • 谨慎使用。所有用户可以操控它,不太安全;而且,它的生命周期长:服务器始到终,存储的数据多了,会造成内存的压力。

  • 代表整个web应用,可以和程序的容器(服务器)通信(交互数据)

获取

  1. 通过request对象获取
  • 调用request.getServletContext();
  1. 通过HttpServlet获取
  • 调用this.getServletContext();

注意:
2个调用方法获取到的ServletContext都是一样的。因此,更多的时候会使用this.getServletContext();

功能

  1. 获取mine类型
  2. 域对象:共享数据
  3. 获取文件的真实路径(服务器路径)

Demo

package cn.itcast.web.servletcontext;

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

@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* ServletContext对象获取:
                1. 通过request对象获取
                 request.getServletContext();
                2. 通过HttpServlet获取
                    this.getServletContext();
         */

        //1. 通过request对象获取
        ServletContext context1 = request.getServletContext();
        //2. 通过HttpServlet获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);

        System.out.println(context1 == context2);//true


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

控制台输出👇

1.获取mine类型

  • 格式:大类型/小类型
  • 比如,text/htmlimage/jpeg 等等

mine类型

  • 互联网通信过程中定义的一种文件数据类型

调用方法

  • String getMimeType(String file)

Demo

package cn.itcast.web.servletcontext;

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

@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

            ServletContext功能:
               1. 获取MIME类型:
                * MIME类型:在互联网通信过程中定义的一种文件数据类型
                    * 格式: 大类型/小类型   text/html		image/jpeg

                * 获取:String getMimeType(String file)
                2. 域对象:共享数据
                3. 获取文件的真实(服务器)路径
         */
        
        //2. 通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //3. 定义文件名称
        String filename = "a.jpg";//image/jpeg


        //4.获取MIME类型
        String mimeType = context.getMimeType(filename);
        System.out.println(mimeType);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

2.域对象

  • 共享数据
  • ServletContext对象范围:所有用户所有请求的数据

调用方法

  1. 设置数据
  • setAttribute(String name,Object value)
  1. 获取数据
  • getAttribute(String name)
  1. removeAttribute(String name)

Demo

ServletContextDemo3.java

package cn.itcast.web.servletcontext;

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

@WebServlet("/servletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

            ServletContext功能:
               1. 获取MIME类型:

                2. 域对象:共享数据
                3. 获取文件的真实(服务器)路径
         */

        //2. 通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //设置数据
        context.setAttribute("msg","haha");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

ServletContextDemo4.java

package cn.itcast.web.servletcontext;

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

@WebServlet("/servletContextDemo4")
public class ServletContextDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

            ServletContext功能:
               1. 获取MIME类型:

                2. 域对象:共享数据
                3. 获取文件的真实(服务器)路径
         */

        //2. 通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //获取数据
        Object msg = context.getAttribute("msg");
        System.out.println(msg);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

3.获取文件的真实路径(服务器路径)

38q0LF.png

调用方法

  • String getRealPath(String path)

Demo

package cn.itcast.web.servletcontext;

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

@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

            ServletContext功能:
               1. 获取MIME类型:

                2. 域对象:共享数据
                3. 获取文件的真实(服务器)路径
         */

        // 通过HttpServlet获取
        ServletContext context = this.getServletContext();


        // 获取文件的服务器路径
        String b = context.getRealPath("/b.txt");//工作空间(IDEA)web目录下资源访问
        System.out.println(b);
        // File file = new File(realPath);

        String c = context.getRealPath("/WEB-INF/c.txt");//工作空间(IDEA)WEB-INF目录下的资源访问
        System.out.println(c);

        String a = context.getRealPath("/WEB-INF/classes/a.txt");//工作空间(IDEA)src目录下的资源访问
        System.out.println(a);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

项目空间、工作空间的区别:视频 进度条【04:52】

注意:

  1. 将来配置文件/资源文件的放置的路径不一样,要会写路径的字符串写法。

  2. src目录的资源路径可以使用ClassLoader类加载器不能获取web目录的路径

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐