ServletContext物件一些功能

2021-09-27 09:41:10 字數 1914 閱讀 9588

1------------sercletcontext物件獲取

/*

sercletcontext物件獲取

1. 通過request物件獲取

request.getservletcontext();

2. 通過httpservlet獲取

this.getservletcontext();

*/servletcontext sc1 = request.getservletcontext();

servletcontext sc2 = this.getservletcontext();

system.out.println(sc1);

system.out.println(sc2);

system.out.println(sc1==sc1);

sercletcontext物件功能

2------------1. 獲取mime型別:

/*

sercletcontext物件功能

1. 獲取mime型別:

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

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

* 獲取:string getmimetype(string file)

*///2.通過httpservlet獲取

servletcontext sc2 = this.getservletcontext();

//3.定義檔名稱

//4獲取mime型別:

string mimetype = getservletcontext().getmimetype(filename);

system.out.println(mimetype);

2------------------. 域物件:共享資料

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

/*

sercletcontext物件功能

2. 域物件:共享資料

1. setattribute(string name,object value)

2. getattribute(string name)

3. removeattribute(string name)

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

*///2.通過httpservlet獲取

servletcontext bc = this.getservletcontext();

//設定資料

bc.setattribute("msg","haha");

//2.通過httpservlet獲取

servletcontext cd = this.getservletcontext();

//獲取資料

object msg = cd.getattribute("msg");

system.out.println(msg);

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

1. 方法:string getrealpath(string path)

//2.通過httpservlet獲取

servletcontext ct = this.getservletcontext();

//servletcontext cd = this.getservletcontext();

string s = ct.getrealpath("/web-inf/classes/a.txt");

system.out.println(s);

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