servlet ServletConfig ServletContext

ServletConfig對象mysql

在Servlet的配置文件中,能夠使用一個或者多個<init-param>標籤爲servlet配置一些初始化參數。程序員

當servlet配置了初始化參數後,web容器在建立servlet實例對象時,會自動將這些初始化參數封裝到ServletConfig對象中國,並在調用servlet的init方法時,將ServletConfig對象傳遞給servlet。進而,程序員經過ServletConfig對象就能夠獲得當前servlet的初始化參數信息。web

 

配置文件web.xml中某個<servlet></servlet>中間有這麼一段:sql

<init-param>數據庫

  <param-name>data</param-name>服務器

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

<init-param>app

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

那麼該servlet實例化的時候會把這些內容一塊兒加載。ui

 

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

相關文章
相關標籤/搜索