ServletConfig與ServletContext對象詳解

ServletConfig與ServletContext對象詳解

一、ServletConfig對象
    在Servlet的配置文件中,可以使用一個或多個<init-param>標籤爲servlet配置一些初始化參數。(配置在某個servlet標籤或者整個web-app下)

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

首先,需要創建私有變量:private ServletConfig config = null;

其次,要重寫init方法,傳入config,令this.config = config;從而獲得ServletConfig對象

最後,就可以獲得<init-parm>中的配置信息了

//獲取初始化參數
  String value1 = this.config.getInitParameter("x1");

//獲得配置文檔中<init-param>標籤下name對應的value
  String vlaue2 = this.config.getInitParameter("x2");
  
  //2.獲取所有的初始化參數(用Enumeration接收)
  Enumeration e = this.config.getInitParameterNames();
  while(e.hasMoreElements()){
   String name = (String) e.nextElement();
   String value = this.config.getInitParameter(name);
   System.out.println(name + "=" + value);
  }

    在開發中ServletConfig的作用有如下三個:

1)獲得字符集編碼

  String charset = this.config.getInitParameter("charset");
2)獲得數據庫連接信息

  String url = this.config.getInitParameter("url");
  String username = this.config.getInitParameter("username");
  String password = this.config.getInitParameter("password");
3)獲得配置文件

  String configFile = this.config.getInitParameter("config");


二、ServletContext對象

  WEB容器在啓動時,它會爲每個WEB應用程序都創建一個對應的ServletContext對象,它代表當前web應用。

  1)ServletContext對象應用1:多個web組件之間使用它實現數據共享

  ServletConfig對象中維護了ServletContext對象的引用,開發人員在編寫servlet時,可以通過 ServletConfig.getServletContext方法獲得ServletContext對象。由於一個WEB應用中的所有Servlet 共享同一個ServletContext對象,因此Servlet對象之間可以通過ServletContext對象來實現通訊。ServletContext對象通常也被稱之爲context域對象。

在serlvet中,可以使用如下語句來設置數據共享

  ServletContext context = this.getServletContext();  //servletContext域對象
  context.setAttribute("data", "共享數據"); //向域中存了一個data屬性

在另一個servlet中,可以使用如下語句來獲取域中的data屬性

  ServletContext context = this.getServletContext();
  String value = (String) context.getAttribute("data");  //獲取域中的data屬性
  System.out.println(value);

  2)通過servletContext對象獲取到整個web應用的配置信息

  String url = this.getServletContext().getInitParameter("url");

  String username = this.getServletContext().getInitParameter("username");
  String password = this.getServletContext().getInitParameter("password");

  3)通過servletContext對象實現servlet轉發

由於servlet中的java數據不易設置樣式,所以serlvet可以將java數據轉發到JSP頁面中進行處理

  this.getServletContext().setAttribute("data","serlvet數據轉發");
  RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/viewdata.jsp");
  rd.forward(request, response);

  4)通過servletContext對象讀取資源文件

  在實際開發中,用作資源文件的文件類型,通常是:xml、properties,而讀取xml文件必然要進行xml文檔的解析,所以以下例子只對properties文件進行讀取(在一個web工程中,只要涉及到寫地址,建議最好以/開頭)

  在web工程中,我們一般來說,是不能採用傳統方式讀取配置文件的,因爲相對的是jvm的啓動目錄(tomcat的bin目錄),所以我們要使用web絕對目錄來獲取配置文件的地址

  讀取資源文件的三種方式:

  第一種:使用ServletContext的getResourceAsStream方法:返回資源文件的讀取字節流

  InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
  Properties prop = new Properties();  

  prop.load(in);
  String url = prop.getProperty("url");

  第二種:使用ServletContext的getRealPath方法,獲得文件的完整絕對路徑path,再使用字節流讀取path下的文件

  String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
  String filename = path.substring(path.lastIndexOf("\\")+1); 

  //相比第一種方法的好處是:除了可以獲取數據,還可以獲取資源文件的名稱
  FileInputStream in = new FileInputStream(path);
  Properties prop = new Properties();
  prop.load(in);
  String url = prop.getProperty("url");

  第三種:使用ServletContext的getResource方法,獲得一個url對象,調用該類的openStream方法返回一個字節流,讀取數據

  URL url = this.getServletContext().getResource("/WEB-INF/classes/db.properties");
  InputStream in = url.openStream();
  Properties prop = new Properties();
  prop.load(in);
  String url1 = prop.getProperty("url");

  5)web工程中,不同位置的資源文件的讀取方式

  一、當資源文件在包下面時
  InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
  System.out.println(in);
  
  二、資源文件在web-inf下
  in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
  System.out.println(in);
  
  三、資源文件在web工程中
  in = this.getServletContext().getResourceAsStream("/db.properties");
  System.out.println(in);

  6)在非servlet程序中如何讀取配置文件:用類裝載器

1)用類裝載方式讀取 

 in = StudentDao.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties");
2)用類裝載方式讀取,把資源當作url對待

 URL url = StudentDao.class.getClassLoader().getResource("db.properties");

 這樣可以獲得資源文件名稱:String path = url.getPath();

3)注意:在線程休眠過程中,即使改動了資源文件,獲取到的還是原始內容

解決方案:

  URL url = StudentDao.class.getClassLoader().getResource("db.properties");
  String path = url.getPath();
  
  FileInputStream in = new FileInputStream(path);
  Properties prop = new Properties();
  prop.load(in);
  System.out.println(prop.getProperty("url"));
  
  try {
   Thread.sleep(1000*15);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  in = new FileInputStream(path);
  prop = new Properties();
  prop.load(in);
  System.out.println(prop.getProperty("url"));

 

4)注意:用類裝載器讀取資源文件時,千萬要注意,資源文件絕對不能太大,否則極易導致內存溢出


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章