關於web.xml

來源:互聯網
上載者:User

標籤:ext   get   檔案   資料   類   資料庫   

1.啟動一個WEB項目的時候,容器(如:Tomcat)會去讀它的設定檔web.xml.讀兩個節 點:<listener></listener> 和 <context-param></context-param>
2.緊接著,容器建立一個ServletContext(上下文),這個WEB項目所有部分都將共用這個上下文.
3.容器將<context-param></context-param>轉化為索引值對,並交給ServletContext.
4.容器建立<listener></listener>中的類執行個體,即建立監聽.
5.在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得
ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的鍵");
6.得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啟動完成.這個動作會比所有的Servlet都要早.
換句話說,這個時候,你對<context-param>中的索引值做的操作,將在你的WEB項目完全啟動之前被執行.
7.舉例.你可能想在項目啟動之前就開啟資料庫.
那麼這裡就可以在<context-param>中設定資料庫的串連方式,在監聽類中初始化資料庫的串連.
8.這個監聽是自己寫的一個類,除了初始化方法,它還有銷毀方法.用於關閉應用前釋放資源.比如說資料庫連接的關閉.
9.以上資料來自<<Head First Servlet&JSP>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.