1. 程式人生 > >ServletConfig與ServletContext物件詳解

ServletConfig與ServletContext物件詳解

ServletConfigServletContext物件詳解

一、ServletConfig物件

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

    當servlet配置了初始化引數後,web容器在建立servlet例項物件時,會自動將這些初始化引數封裝到ServletConfig物件中,並在呼叫servletinit方法時,將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應用。

  1ServletContext物件應用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物件讀取資原始檔

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

  在web工程中,我們一般來說,是不能採用傳統方式讀取配置檔案的,因為相對的是jvm的啟動目錄(tomcatbin目錄),所以我們要使用web絕對目錄來獲取配置檔案的地址

  讀取資原始檔的三種方式:

  第一種:使用ServletContextgetResourceAsStream方法:返回資原始檔的讀取位元組流

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

  Properties prop = new Properties();  

  prop.load(in);

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

  第二種:使用ServletContextgetRealPath方法,獲得檔案的完整絕對路徑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");

  第三種:使用ServletContextgetResource方法,獲得一個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");

  5web工程中,不同位置的資原始檔的讀取方式

  一、當資原始檔在包下面時

  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)注意:用類裝載器讀取資原始檔時,千萬要注意,資原始檔絕對不能太大,否則極易導致記憶體溢位