web.xml上下文配置

來源:互聯網
上載者:User

標籤:注意   view   strong   let   tom   ref   get   list   上下   

  •  格式定義:
[html] view plaincopy 
  1. <context-param>  
  2. <param-name>contextConfigLocation</param-name>  
  3. <param-value>contextConfigLocationValue></param-value>  
  4. </context-param>  

作用:該元素用來聲明應用範圍(整個WEB項目)內的上下文初始化參數。

param-name 設定內容相關的參數名稱。必須是唯一名稱

param-value 設定的參數名稱的值

  • 初始化過程:
    1. 在啟動Web項目時,容器(比如Tomcat)會讀web.xml設定檔中的兩個節點<listener>和<contex-param>。
    2. 接著容器會建立一個ServletContext(上下文),應用範圍內即整個WEB項目都能使用這個上下文。
    3. 接著容器會將讀取到<context-param>轉化為索引值對,並交給ServletContext。
    4. 容器建立<listener></listener>中的類執行個體,即建立監聽(備忘:listener定義的類可以是自訂的類但必須需要繼承ServletContextListener)。
    5. 在監聽的類中會有一個contextInitialized(ServletContextEvent event)初始化方法,在這個方法中可以通過event.getServletContext().getInitParameter("contextConfigLocation") 來得到context-param 設定的值。在這個類中還必須有一個contextDestroyed(ServletContextEvent event) 銷毀方法.用於關閉應用前釋放資源,比如說資料庫連接的關閉。
    6. 得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啟動完成.這個動作會比所有的Servlet都要早。

由上面的初始化過程可知容器對於web.xml的載入過程是context-param >> listener  >> fileter  >> servlet

  •  如何使用
  1. 頁面中

    ${initParam.contextConfigLocation}

  2. Servlet中
    String paramValue=getServletContext().getInitParameter("contextConfigLocation")

 

web.xml上下文配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.