ServletContext 对象

本文围绕 ServletContext 展开,介绍其代表整个应用,可与服务器通信及两种获取对象的方式。重点阐述其功能,包括获取 MIME 类型、作为域对象共享数据(但使用需慎重)以及获取文件真实(服务器)路径,并给出相应代码示例和输出结果。


一、ServletContext 概述

ServletContext概念:代表整个web应用,可以和程序的容器(服务器)来通信

共有两种获取ServletContext对象的方式:

  • 通过request对象获取:request.getServletContext();
  • 通过HttpServlet对象获取:this.getServletContext();

二、功能

1、获取 MIME 类型

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

它的格式是:大类型/小类型,例如:text/html或者image/jpeg

通过方法String getMimeType(String file)来获取

如下代码演示了如何获取MIME类型:

@WebServlet("/contextDemo01")
public class ContextDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 通过HttpServlet来获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2. 定义一个文件的名称
        String filename = "a.jpg";  // image/jpeg
        //3.获取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、域对象:共享数据

关于域对象,我在之前的博客里面提到过,ServletContextrequest对象的设置域对象、获取域对象、移除域对象的方法一模一样:

  • 设置域对象:setAttribute(String name,Object value)
  • 获取域对象:getAttribute(String name)
  • 移除域对象:removeAttribute(String name)

但是ServletContext域对象的范围包括所有用户请求的数据,它的生命周期从服务器开启一直到服务器关闭才可以结束,而且所有用户都可以操控,这也意味着ServletContext对象不安全,用的时候千万要慎重!

下面演示一下:

新建一个Servlet,这里写设置域对象,写入以下代码:

@WebServlet("/contextDemo02")
public class ContextDemo02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2. 设置域对象
        context.setAttribute("msg","I am here!");
    }

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

再新建一个Servlet,这里获取域对象:

@WebServlet("/contextDemo03")
public class ContextDemo03 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2. 获取域对象
        Object msg = context.getAttribute("msg");
        System.out.println(msg);
    }

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

代码完成,启动Tomcat服务器,然后首先访问第一个Servlet,接着访问第二个Servlet,输出结果如下所示:
在这里插入图片描述

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

首先我们在项目里面创建三个文件,分别放在不同的位置,如下图所示:
在这里插入图片描述
然后新建一个Servlet,写代码:

@WebServlet("/contextDemo04")
public class ContextDemo04 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2. 获取web目录下的资源访问,b.txt
        String bpath = context.getRealPath("/b.txt");
        System.out.println("b.txt路径:" + bpath);
        //3. 获取WEB-INF目录下的资源访问 c.txt
        String cpath = context.getRealPath("/WEB-INF/c.txt");
        System.out.println("c.txt路径:" + cpath);
        //4. 获取src目录下的资源访问 a.txt
        String apath = context.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println("a.txt路径:" + apath);
    }

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

输出结果:
在这里插入图片描述
这样就输出出来啦~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值