ServletContext基礎(還在更新中)

2021-07-11 01:40:03 字數 1111 閱讀 4856

web 容器在啟動時會載入每乙個web 應用程式。

1.      servletcontext (servlet 上下文物件):每個web工程都有自己的servletcontext物件。

範圍:應用程式範圍。可以被web 應用程式的所有jsp頁面和servlet 訪問。

2.      servletconfig(servlet 配置物件):每個servlet 都有自己的servletconfig物件。

範圍:servlet 範圍。

3.       

servletcontext 物件是在web 應用程式裝載時初始化的。

得到servletcontext物件的方法:

1.  servletcontext  context  = getservletcontext();

2.  servletcontext  context  = getservletconfig().getservletcontext();

得到servletcontext 物件引數的方法:

1.      public  string getinitparameter(string name);  name 為指定的引數值,若不存在,返回null。

2.      public  enumeration getinitparameternames();  返回所有的引數。

得到servletcontext物件引數值得方法:

servletcontext  context =  getservletcontext();

string value  =  context.getinitparameter(「name」);

通過servletcontext物件獲得資源的方法:

1.   public  url getresource(string  path);必須以『

/』開頭,它相對於

web應用程式的根目錄。

2.   public inputstream getresourceasstream(string  path);相當於getresource(path).openstream()。

3.   public  string getrealpath(string  path);返回給定路徑的絕對路徑。

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