日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ServletContext的学习笔记(属Servlet学习课程)

發(fā)布時間:2023/12/3 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ServletContext的学习笔记(属Servlet学习课程) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • ServletContext 簡介
    • 獲取 ServletContext 對象
    • 功能
      • 獲取 MIME 類型
      • 用來共享數(shù)據(jù)
      • 獲取文件在服務(wù)器主機的物理路徑
      • 獲取全局級別的初始化參數(shù)
      • 獲取臨時目錄

ServletContext 簡介

web 服務(wù)器在啟動時,會為每一個已經(jīng)部署的應(yīng)用創(chuàng)建唯一的一個 ServletContext 實例,ServletContext 實例對象代表整個 Web 應(yīng)用,可以和 Servlet 的容器(服務(wù)器)通信。

該實例會一直存在,除非服務(wù)器關(guān)閉或者應(yīng)用被刪除。

注意:每個應(yīng)用對應(yīng)唯一的一個 ServletContext 實例

獲取 ServletContext 對象

GenericServlet 提供了getServletContext() 方法
HttpSession 提供了getServletContext() 方法
ServletConfig 提供了getServletContext() 方法

在 Servlet 中獲取 ServletContext 對象:
1.request.getServletContext()
2.this.getServletContext(),因為自定義的 Servlet 繼承了來自 GenericServlet 的 getServletContext() 方法。

功能

獲取 MIME 類型

一種互聯(lián)網(wǎng)通信的文件數(shù)據(jù)類型
格式:大類型/小類型 例如:text/html、image/jpeg

ServletContext 可以獲取文件的 MIME 類型,實際上 MIME 類型存儲在服務(wù)器的 conf/web.xml 文件中,因為 ServletContext 可以和服務(wù)器通信,所以可以獲取 MIME 數(shù)據(jù)。

客戶端會根據(jù) MIME 類型使用對應(yīng)的解析引擎來解析響應(yīng)體的數(shù)據(jù)。

示例代碼:

package priv.lwx.javaex.servlet_demo.web.servletcontext; /*** 獲取MIME類型** @author liaowenxiong* @date 2022/1/12 15:34*/import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.File; import java.io.IOException;@WebServlet("/servlet_context_demo01") public class ServletContextDemo01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {File file = new File("druid.properties");String name = file.getName();System.out.println(name);ServletContext context = this.getServletContext();String mimeType = context.getMimeType(name);System.out.println(mimeType); // 結(jié)果為null,properties文件沒有對應(yīng)的MIME類型String name1 = "a.jpeg";String mimeType1 = context.getMimeType(name1);System.out.println(mimeType1); // 結(jié)果為image/jpeg} }

用來共享數(shù)據(jù)

ServletContext 對象也是一個域?qū)ο?#xff0c;可以用來共享數(shù)據(jù)。

setAttribute(String name, Object value);
getAttribute(String name);
removeAttribute(String name);

數(shù)據(jù)共享范圍:所有用戶的所有請求的數(shù)據(jù)。比方說,張三把自己的數(shù)據(jù)往 ServletContext 對象存放,李四可以獲取到張三存放的數(shù)據(jù),李四也可以存放自己的數(shù)據(jù)到 ServletContext 對象中,張三也可以獲取李四存放的數(shù)據(jù)。

生命周期:服務(wù)器啟動就創(chuàng)建 ServletContext 對象,服務(wù)器關(guān)閉了 ServletContext 對象被銷毀。

注:謹慎使用 ServletContext 對象存放數(shù)據(jù),因為共享范圍太多,數(shù)據(jù)不安全;另外 ServletContext 對象存活周期太長,存放數(shù)據(jù)過多,會對服務(wù)器內(nèi)存造成壓力。

獲取文件在服務(wù)器主機的物理路徑

方法:getRealPath(String path)

調(diào)用 ServletContext 對象的 getRealPath(String path) 方法可以獲取指定文件在服務(wù)器主機的實際路徑。實際這個方法只是在你給定的路徑(也叫邏輯路徑)前面加上項目的根目錄路徑而已。例如,我的項目的根目錄路徑是 /Users/liaowenxiong/Documents/IdeaProjects/java-exercises/out/artifacts/servlet_demo_war_exploded,如果你給定的路徑 /WEB-INF/classes/druid.properties,那么會返回給你 /Users/liaowenxiong/Documents/IdeaProjects/java-exercises/out/artifacts/servlet_demo_war_exploded/WEB-INF/classes/druid.properties,如果你給定的路徑是 druid.properties,那么會返回給你 /Users/liaowenxiong/Documents/IdeaProjects/java-exercises/out/artifacts/servlet_demo_war_exploded/druid.properties

注意:
1.文件在 src 目錄下或者 resources 目錄下(maven工程),getRealPath(String path) 中的 path 你要寫成 /WEB-INF/classes/文件名
2.文件在 web 目錄下或者 webapp 目錄下,你要寫成 /文件名
3.文件在 WEB-INF 目錄下,你要寫成 /WEB-INF/文件名

另外一種獲取文件真實路徑的方式:

// 通過類加載器獲取文件的真實路徑 URL url = ServletContextDemo02.class.getClassLoader().getResource("/druid.properties"); // 也可以去掉斜桿 String path = url.getPath(); System.out.println(path);

特別注意:getResource(String name),中 name 是文件的名稱,文件只能在 src 目錄或者 resources 目錄下,其它位置下的文件無法識別到。

獲取全局級別的初始化參數(shù)

step1
在 web.xml 中,使用 <context-param> 配置的參數(shù),可以被所有的 Servlet 共享。

step2

通過 ServletContext 獲取全局參數(shù)的值:

ServletContext servletContext = getServletContext(); String version = servletContext.getInitParameter("version");

獲取臨時目錄

可以把客戶端上傳的文件緩存在這個臨時目錄下,這個目錄下的數(shù)據(jù)容器會自動清理掉。

File file0 = (File) sctx.getAttribute("javax.servlet.context.tempdir"); System.out.println(file0);

總結(jié)

以上是生活随笔為你收集整理的ServletContext的学习笔记(属Servlet学习课程)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。