ServletContext的学习笔记(属Servlet学习课程)
文章目錄
- ServletContext 簡(jiǎn)介
- 獲取 ServletContext 對(duì)象
- 功能
- 獲取 MIME 類型
- 用來(lái)共享數(shù)據(jù)
- 獲取文件在服務(wù)器主機(jī)的物理路徑
- 獲取全局級(jí)別的初始化參數(shù)
- 獲取臨時(shí)目錄
ServletContext 簡(jiǎn)介
web 服務(wù)器在啟動(dòng)時(shí),會(huì)為每一個(gè)已經(jīng)部署的應(yīng)用創(chuàng)建唯一的一個(gè) ServletContext 實(shí)例,ServletContext 實(shí)例對(duì)象代表整個(gè) Web 應(yīng)用,可以和 Servlet 的容器(服務(wù)器)通信。
該實(shí)例會(huì)一直存在,除非服務(wù)器關(guān)閉或者應(yīng)用被刪除。
注意:每個(gè)應(yīng)用對(duì)應(yīng)唯一的一個(gè) ServletContext 實(shí)例
獲取 ServletContext 對(duì)象
GenericServlet 提供了getServletContext() 方法
HttpSession 提供了getServletContext() 方法
ServletConfig 提供了getServletContext() 方法
在 Servlet 中獲取 ServletContext 對(duì)象:
1.request.getServletContext()
2.this.getServletContext(),因?yàn)樽远x的 Servlet 繼承了來(lái)自 GenericServlet 的 getServletContext() 方法。
功能
獲取 MIME 類型
一種互聯(lián)網(wǎng)通信的文件數(shù)據(jù)類型
格式:大類型/小類型 例如:text/html、image/jpeg
ServletContext 可以獲取文件的 MIME 類型,實(shí)際上 MIME 類型存儲(chǔ)在服務(wù)器的 conf/web.xml 文件中,因?yàn)?ServletContext 可以和服務(wù)器通信,所以可以獲取 MIME 數(shù)據(jù)。
客戶端會(huì)根據(jù) MIME 類型使用對(duì)應(yīng)的解析引擎來(lái)解析響應(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文件沒(méi)有對(duì)應(yīng)的MIME類型String name1 = "a.jpeg";String mimeType1 = context.getMimeType(name1);System.out.println(mimeType1); // 結(jié)果為image/jpeg} }用來(lái)共享數(shù)據(jù)
ServletContext 對(duì)象也是一個(gè)域?qū)ο?#xff0c;可以用來(lái)共享數(shù)據(jù)。
setAttribute(String name, Object value);
getAttribute(String name);
removeAttribute(String name);
數(shù)據(jù)共享范圍:所有用戶的所有請(qǐng)求的數(shù)據(jù)。比方說(shuō),張三把自己的數(shù)據(jù)往 ServletContext 對(duì)象存放,李四可以獲取到張三存放的數(shù)據(jù),李四也可以存放自己的數(shù)據(jù)到 ServletContext 對(duì)象中,張三也可以獲取李四存放的數(shù)據(jù)。
生命周期:服務(wù)器啟動(dòng)就創(chuàng)建 ServletContext 對(duì)象,服務(wù)器關(guān)閉了 ServletContext 對(duì)象被銷毀。
注:謹(jǐn)慎使用 ServletContext 對(duì)象存放數(shù)據(jù),因?yàn)楣蚕矸秶?#xff0c;數(shù)據(jù)不安全;另外 ServletContext 對(duì)象存活周期太長(zhǎng),存放數(shù)據(jù)過(guò)多,會(huì)對(duì)服務(wù)器內(nèi)存造成壓力。
獲取文件在服務(wù)器主機(jī)的物理路徑
方法:getRealPath(String path)
調(diào)用 ServletContext 對(duì)象的 getRealPath(String path) 方法可以獲取指定文件在服務(wù)器主機(jī)的實(shí)際路徑。實(shí)際這個(gè)方法只是在你給定的路徑(也叫邏輯路徑)前面加上項(xiàng)目的根目錄路徑而已。例如,我的項(xiàng)目的根目錄路徑是 /Users/liaowenxiong/Documents/IdeaProjects/java-exercises/out/artifacts/servlet_demo_war_exploded,如果你給定的路徑 /WEB-INF/classes/druid.properties,那么會(huì)返回給你 /Users/liaowenxiong/Documents/IdeaProjects/java-exercises/out/artifacts/servlet_demo_war_exploded/WEB-INF/classes/druid.properties,如果你給定的路徑是 druid.properties,那么會(huì)返回給你 /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/文件名
另外一種獲取文件真實(shí)路徑的方式:
// 通過(guò)類加載器獲取文件的真實(shí)路徑 URL url = ServletContextDemo02.class.getClassLoader().getResource("/druid.properties"); // 也可以去掉斜桿 String path = url.getPath(); System.out.println(path);特別注意:getResource(String name),中 name 是文件的名稱,文件只能在 src 目錄或者 resources 目錄下,其它位置下的文件無(wú)法識(shí)別到。
獲取全局級(jí)別的初始化參數(shù)
step1
在 web.xml 中,使用 <context-param> 配置的參數(shù),可以被所有的 Servlet 共享。
step2
通過(guò) ServletContext 獲取全局參數(shù)的值:
ServletContext servletContext = getServletContext(); String version = servletContext.getInitParameter("version");獲取臨時(shí)目錄
可以把客戶端上傳的文件緩存在這個(gè)臨時(shí)目錄下,這個(gè)目錄下的數(shù)據(jù)容器會(huì)自動(dòng)清理掉。
File file0 = (File) sctx.getAttribute("javax.servlet.context.tempdir"); System.out.println(file0);總結(jié)
以上是生活随笔為你收集整理的ServletContext的学习笔记(属Servlet学习课程)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 公司内网怎么设置无线路由器内网如何转无线
- 下一篇: mt8125相当于(mt8173和817