servlet ServletConfig ServletContext

劍握在手發表於2013-11-29

ServletConfig物件

在Servlet的配置檔案中,可以使用一個或者多個<init-param>標籤為servlet配置一些初始化引數。

當servlet配置了初始化引數後,web容器在建立servlet例項物件時,會自動將這些初始化引數封裝到ServletConfig物件中國,並在呼叫servlet的init方法時,將ServletConfig物件傳遞給servlet。進而,程式設計師通過ServletConfig物件就可以得到當前servlet的初始化引數資訊。

 

配置檔案web.xml中某個<servlet></servlet>中間有這麼一段:

<init-param>

  <param-name>data</param-name>

  <parm-value>xxxxx</param-value>

<init-param>

<!--<load-on-startup>2</load-on-startup>-->

那麼該servlet例項化的時候會把這些內容一起載入。

 

servlet中的程式碼(複寫了init):

……entends HttpServlet{

  private ServletConfig config;

……doGet……

  String value = config.getInitParameter("data");//得到xxxxx

……

……doPost……

  

public void init(ServletConfig config)throws ServletExceptio{

  //super.init(config);

  this.config = config;

}

其實父類HttpServlet的父類GenericSrvlet中已經定義了這麼個私有的東西,並在其init方法中獲取了。還提供了一個獲取方法:

public ServletConfig getServletConfig(){

  return config;

}

我們可以在自己的類中直接用這個方法獲得。

 

得到所有:

Enumeration e = this.getServletConfig().getInitParameterNames();

whie(e.hasMoreElements()){

  String name = (String)e.nexElement();

  String value = this.getServletConfig().getInitParameter(name);

}

 

用到的場合。

比如:

解碼方式可以配置一個

<init-param>

  <param-name>charset</param-name>

  <param-value>UTF-8</param-value>

</init-param>

 

ServletContext

web容器在啟動時,它會為每個web應用程式都建立一個對應的ServletContext物件,它代表當前web應用。

ServletConfig物件中維護了ServletContext物件的引用,開發人員在編寫servlet時,可以通過ServletConfig.getServletContext方法獲得ServletContext物件。

獲取方式:

方式一,

ServletContext context = this.getServletConfig().getServletContext();

方式二,

context = this.getServletContext();

 

ServletContext的應用:

由於一個web應用中所有的Servlet共享同一個ServletContext物件,所以多個Servlet通過ServletContext物件實現資料共享。

ServletContext物件通常也稱之為context物件。

域:範圍。 context域物件:作用範圍是整個應用程式的物件。除此之外還有request session page域物件。

一個Servlet1中有如下程式碼:

String data ="aaa";

this.getServletContext().setAttribute("data",data);

另一個Servlet2中有如下程式碼:

String value = (String)this.getServletContext().getAttribute("data");

當Servlet1執行過,Servlet2就可以獲取aaa。

比如聊天室就可以用這個servltContext。

 

web.xml中的<web-app></web-app>中間可以有這麼一段:

<context-param>

  <param-name>data</param-name>

  <param-value>xxxx</param-value>

  <param-name>data2</param-name>

  <param-value>xxxx2</param-value>

</context-param>

載入該web應用就會自動把該引數載入。

在程式中獲取:

this.getServletContext().getInitParameterNames();和ServletConfig類似。

在配置檔案中配置改起來很方便。如果在程式中改要改程式碼,重新編譯比較麻煩。

  

ServletContext還實現了Servlet的轉發:

String data = "abc";

data = "<meta http-equiv='fresh' content='3;url=/myweb/index.jsp'>3秒後將跳轉";

//this.getServletContext().setAttribute("data",data);由於servlet是單例子的,所以這樣有可能最後加進去的data是bcd而非abc,所以這種設定全域性的方式不要採用

//不過可以在jsp頁面通過獲取全域性某個屬性的方式顯示這個data:<% String str = (String)Applcation.getAttribute("data");out.write(str); %>

RequestDispatcher rd = this.getServletContext().getRequestDispatcher("1.jsp");

rd.forward(request,response);//頁面將轉至1.jsp頁面,與重定向不同,這樣僅請求了一次伺服器,而重定向是兩次。

 

Servletcontext讀取資原始檔.properties檔案:

關於properties檔案:

properties和xml都是資原始檔,不同之處是xml一般用來存有關係的資料,而properties檔案則存放類似使用者名稱、密碼、資料庫連線地址之類沒有關係的資料。

properties檔案內容樣例:

url=jdbc:mysql://localhost:3306/test

username=admin

password=admin

如上面所寫要帶個等號。

 

下面寫一下

  Servletcontext讀取資原始檔程式碼示例:

Inputstream in = this.getServletContext().getResourceAdStrem("/db.properties");

關於這個相對路徑的書寫,用context讀取,相對路徑中的第一個"/"表示web應用目錄。例如,你在開發的時候把properties放在src下,那麼部署到伺服器後會出現在“根目錄/WEB-INF/classes/”下,這個時候應該寫“/WEB-INF/classes/xx.properties”。如果直接用讀取流,那麼相對路徑就變成了啟動Tomcat伺服器的目錄,即Tomcat下的bin目錄。

Properties props = new Properties();//map

props.load(in);

String url = props.getProperty("url");//獲取鍵值

 

更多內容參見API

相關文章