Java ServletContext對象用法解析
ServletContext對象:
ServletContext類似字節(jié)碼文件對象,在web創(chuàng)建的時候就自動生成了,并且是唯一的,跟隨著項目和服務(wù)器共存亡了。通過這個對象,我們可以向里面存數(shù)據(jù)(鍵值對),也可以通過別的Servlet來獲取這個數(shù)據(jù);也可以根據(jù)相對(服務(wù)器)路徑繼來獲取絕對路徑。
ServletContext代表是一個web應(yīng)用的環(huán)境(上下文)對象,ServletContext對象內(nèi)部封裝是該web應(yīng)用的信息,ServletContext對象一個web應(yīng)用只有一個。 一個web應(yīng)用有幾個servlet對象?----多個
ServletContext對象的生命周期?
創(chuàng)建:該web應(yīng)用被加載(服務(wù)器啟動或發(fā)布web應(yīng)用(前提,服務(wù)器啟動狀態(tài)))
銷毀:web應(yīng)用被卸載(服務(wù)器關(guān)閉,移除該web應(yīng)用)
獲得ServletContext對象:
第一種:Servlet的init方法中獲得ServletConfig 初始化方法
ServletContext servletContext = config.getServletContext ();
第二種:
ServletContext servletContext = config.getServletContext ();
ServletContext的作用:
獲得web應(yīng)用中任何資源的絕對路徑(重要 重要 重要)
String path = context.getRealPath(相對于該web應(yīng)用的相對地址);
public class Servlet01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取ServletContext對象
ServletContext context =getServletContext();
//獲取相對于服務(wù)器的相對路徑獲取絕對路徑
String patha=context.getRealPath("WEB-INF/classes/a.txt");
String pathb=context.getRealPath("b.txt");
String pathc=context.getRealPath("WEB-INF/c.txt");
//d.txt創(chuàng)建在WEB04文件下,不會在服務(wù)器上找到的。以后靜態(tài)資源創(chuàng)建在WebContent下,項目文件、配置文件在src下
System.out.println(patha);
System.out.println(pathb);
System.out.println(pathc);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
ServletContext是一個域?qū)ο螅ù鎯?shù)據(jù)的區(qū)域):
存儲數(shù)據(jù)的區(qū)域就是域?qū)ο螅?/p>
ServletContext域?qū)ο蟮淖饔梅秶赫麄€web應(yīng)用(所有的web資源都可以隨意向 servletcontext域中存取數(shù)據(jù),數(shù)據(jù)可以共享)
域?qū)ο蟮耐ㄓ玫姆椒ǎ?br />
setAtrribute(String name,Object obj); k是字符串 value是obj類型
getAttribute(String name); 強(qiáng)轉(zhuǎn)
removeAttribute(String name);
public class Serlvlet03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取ServletContext對象
ServletContext context=getServletContext();
//獲取ServletContext域中的值
String name=(String)context.getAttribute("name");
response.getWriter().write(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中Controller引起的Ambiguous?mapping問題及解決
這篇文章主要介紹了Java中Controller引起的Ambiguous?mapping問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Java數(shù)據(jù)結(jié)構(gòu)之鏈表詳解
本篇文章我們將講解一種新型的數(shù)據(jù)結(jié)構(gòu)—鏈表,鏈表是一種使用廣泛的通用數(shù)據(jù)結(jié)構(gòu),它可以用來作為實現(xiàn)棧,隊列等數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ).文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-05-05
SpringCloud Feign多參數(shù)傳遞及需要注意的問題
這篇文章主要介紹了SpringCloud Feign多參數(shù)傳遞及需要注意的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
解析ConcurrentHashMap: 預(yù)熱(內(nèi)部一些小方法分析)
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06

