1. 程式人生 > 其它 >ServletContext 物件

ServletContext 物件

技術標籤:JavaWebjavaservlettomcatMIMEj2ee

文章目錄


一、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);
    }
}

輸出結果:
在這裡插入圖片描述
這樣就輸出出來啦~