ServletContext學前必看 十分詳細

2021-09-29 16:48:09 字數 1253 閱讀 6754

1. 概念:代表整個web應用,可以和程式的容器(伺服器)來通訊

2. 獲取:

//1. 通過request物件獲取

request.

getservletcontext()

;//2. 通過httpservlet獲取

this

.getservletcontext()

;

3. 功能:

1. 獲取mime型別:

mime型別:在網際網路通訊過程中定義的一種檔案資料型別

格式: 大型別/小型別 text/html image/jpeg

獲取:string getmimetype(string file)

2. 域物件:共享資料

setattribute

(string name,object value)

getattribute

(string name)

removeattribute

(string name)

servletcontext物件範圍:所有使用者所有請求的資料

3. 獲取檔案的真實(伺服器)路徑

方法:string getrealpath(string path)

string b = context.

getrealpath

("/b.txt");

//web目錄下資源訪問

system.out.

println

(b);

string c = context.

getrealpath

("/web-inf/c.txt");

//web-inf目錄下的資源訪問

system.out.

println

(c);

string a = context.

getrealpath

("/web-inf/classes/a.txt");

//src目錄下的資源訪問

system.out.

println

(a);

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...