ServletContext介紹及用法

2021-09-02 05:54:39 字數 3401 閱讀 8289

1.1.  介紹

servletcontext官方叫servlet上下文。伺服器會為每乙個工程建立乙個物件,這個物件就是servletcontext物件。這個物件全域性唯一,而且工程內部的所有servlet都共享這個物件。所以叫全域性應用程式共享物件。

1.2.  作用

1.      是乙個域物件

2.      可以讀取全域性配置引數

3.      可以搜尋當前工程目錄下面的資源檔案

4.      可以獲取當前工程名字(了解)

1.2.1.   servletcontext是乙個域物件

1.2.1.1.           域物件介紹

域物件是伺服器在記憶體上建立的儲存空間,用於在不同動態資源(servlet)之間傳遞與共享資料。

1.2.1.2.           域物件方法

凡是域物件都有如下3個方法:

setattribute(name,value);name是string型別,value是object型別;

往域物件裡面新增資料,新增時以key-value形式新增

getattribute(name);

根據指定的key讀取域物件裡面的資料

removeattribute(name);

根據指定的key從域物件裡面刪除資料

1.2.1.3.           域物件功能**

域物件儲存資料adddataservlet**

//往serlvetcontext裡面存資料

//1.獲取servletcontext物件

//getservletcontext()

//2.往物件裡面設定資料

getservletcontext().setattribute("username", "admin");

response.getoutputstream().write("使用者名稱寫入到servletcontext成功".getbytes());

獲取域物件資料getdataservlet**

//獲取servletcontext裡面的使用者名稱資料

object valueobject = getservletcontext().getattribute("username");

if(valueobject!=null){

response.getoutputstream().write(("從servletcontext讀取到的使用者名稱資料:"+valueobject.tostring()).getbytes());

servletcontext儲存資料特點,

全域性共享,裡面的資料所有動態資源都可以寫入和獲取

伺服器啟動的時候建立,伺服器關閉的時候銷毀,因為這是全域性應用程式物件,全域性共享物件。

1.2.2.   可以讀取全域性配置引數

1.2.2.1.           servletcontext讀取全域性引數核心方法

getservletcontext().getinitparameter(name);//根據指定的引數名獲取引數值

getservletcontext().getinitparameternames();//獲取所有引數名稱列表

1.2.2.2.           實現步驟:

1.      在web.xml中配置全域性引數

param1

value1

param2

value2

2.      在動態資源servlet裡面使用servletcontext讀取全域性引數**

//使用servletcontext讀取全域性配置引數資料

//核心方法

/*getservletcontext().getinitparameter(name);//根據指定的引數名獲取引數值

getservletcontext().getinitparameternames();//獲取所有引數名稱列表*/

//列印所有引數

//1.先獲取所有全域性配置引數名稱

enumerationenumeration =  getservletcontext().getinitparameternames();

//2.遍歷迭代器

while(enumeration.hasmoreelements()){

//獲取每個元素的引數名字

string paramename = enumeration.nextelement();

//根據引數名字獲取引數值

string paramevalue = getservletcontext().getinitparameter(paramename);

//列印

system.out.println(paramename+"="+paramevalue);

1.2.3.   可以搜尋當前工程目錄下面的資源檔案

1.2.3.1.           核心方法

getservletcontext().getrealpath(path),根據相對路徑獲取伺服器上資源的絕對路徑

getservletcontext().getresourceasstream(path),根據相對路徑獲取伺服器上資源的輸入位元組流

1.2.4.   可以獲取當前工程名字

1.2.4.1.           核心方法

getservletcontext().getcontextpath();

作用:獲取當前工程名字

//獲取工程名字,getservletcontext().getcontextpath()

response.getoutputstream().write(("工程名字:"+getservletcontext().getcontextpath()).getbytes());

原文: 

ServletContext介面功能

使用getinitparameternames 和getinitparameter string name 來獲得web應用中的初始化引數 setattribute string name,string value 來設定共享的資料 getattribute string name 來獲得共享得資料...

ServletContext物件學習

問題 不同的使用者使用相同的資料 解決 servletcontext物件 特點 伺服器建立 使用者共享 作用域 整個專案內 生命週期 伺服器啟動到伺服器關閉 使用 獲取servletcontext物件 第一種方式 servletcontext sc this.getservletcontext 第二...

ServletContext物件的應用

servletcontext物件 user root pass 1234 1.獲取指定名稱的context配置資訊 servletcontex context this.getservletcontext string user context.getinitparameter user syste...