ServletContext

什麼是ServletCotext?   html

 在應用被服務器加載時就建立ServletContext對象的實例。每個JavaWeb應用都有惟一的一個ServletContext對象
它就表明着當前的應用。web

怎麼獲得ServletContext?服務器

  ServletConfig維護了ServletContext對象的應用.全部就能夠經過ServletConfig獲取ServletContext對象 getServletContext()spa

ServletContext的應用?code

  一、由於全部Servlet共享一個Servlet對象.所以Servlet能夠經過ServletContext來實現通信。因此ServletContext也稱之爲域對象(內部維護了一個                    Map<String,Object>)xml

    實現Servlet直接的數據共享htm

    Object getAttribute(String name):根據名稱獲取綁定的對象對象

    Enumeration getAttributeNames() :獲取ServletContext域中的全部名稱blog

    void removeAttribute(String name):根據名稱移除對象
    void setAttribute(String name,Object value):添加或修改對象。資源

  二、獲取WEB應用的初始化參數。

  在web.xml下面配置

  <init-param>

    <param-name></param-name>

    <param-value></param-value>

  </init-param>

  經過ServletContext讀取

  三、實現轉發

  四、利用ServletContext讀取資源文件

  

  利用ServletContext.getRealPath():
  特色:讀取應用中任何文件。只能在Web環境下用
  利用ResourceBundle讀取配置文件
  特色:能夠用在非web環境下。可是隻能讀取類路徑中的properties文件
  利用類加載器讀取配置文件(專業)
  特色:能夠用在非web環境下。能夠讀取類路徑下的任何文件

TIPS:

http://www.cnblogs.com/legend-liu/p/3860559.html ServletConfig對象

相關文章
相關標籤/搜索