瞭解Web.xml的配置,Web.xml配置

來源:互聯網
上載者:User

瞭解Web.xml的配置,Web.xml配置

          web.xml是web項目啟動時首先被容器讀取的設定檔,根據其中的配置建立執行個體並完成參數初始化等以保證項目能夠正確啟動運行.web.xml中配置的項目主要有這麼幾種,我對各個元素的作用瞭解如下:


上下文參數:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:/config/applicationContext-*.xml</param-value>  </context-param>


         這一元素用來聲明應用範圍(整個web項目)內的上下文初始化參數.參數名稱是唯一的,就是固定不變的.在web.xml中通過contextConfigLocation配置spring,contextConfigLocation參數定義了要裝入的 Spring設定檔.如果預設這種配置,預設載入/WEB-INF/applicationContext.xml檔案,如果有多個,param-value裡可以有多個設定檔名稱,並以逗號分隔.

 

監聽器:

<listener>    <listener-class>com.tgb.itoo.MyListener</listener-class>  </listener>

         listener元素用來註冊一個監聽器類,listener-class指定監聽類.

servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知.監聽類繼承ServletContextListener,在監聽的類中有一個contextInitialized(ServletContextEventevent)初始化方法,在這個方法中可以通過event.getServletContext().getInitParameter("contextConfigLocation")來得到context-param 設定的值.在這個類中還必須有一個contextDestroyed(ServletContextEventevent) 銷毀方法.用於關閉應用前釋放資源,比如說資料庫連接的關閉.

 

 

過濾器:

<filter>            <filter-name>setCharacterEncoding</filter-name>            <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>            <init-param>                <param-name>encoding</param-name>                <param-value>GB2312</param-value>            </init-param>    </filter>    <filter-mapping>            <filter-name>setCharacterEncoding</filter-name>            <url-pattern>/*</url-pattern>    </filter-mapping>  


        將一個名字與一個實現javax.servlet.Filter介面的類相關聯. <url-pattern>定義Filter攔截的URL地址,說明攔截請求的方式.

 

 

servlet配置:


<servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/spring-mvc.xml,classpath:config/applicationContext-common.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>

       

         該元素指定了servlet的名稱和servlet的類名稱.Web伺服器與Web應用屬於兩個範疇,要使我們的web應用在Web伺服器上發布和運行,需要標準的中間介面,Servlet就是其中之一,所以這樣的Web伺服器也叫做Servlet容器.我們看到<servlet>中有<param-name>contextConfigLocation</param-name>的配置,而上面<context-param>中也有<param-name>contextConfigLocation</param-name>,雖然名稱相同,但應用範圍不同,分別是ServletConfig和ServletContext.具體差異之後的部落格中會給出解釋.

 

web.xml是如何被讀取的以及容器讀取設定檔過程中都進行了哪些操作,簡單分析如下:


  1. 在啟動Web項目時,容器(如Tomcat)會讀web.xml設定檔中的兩個節點<listener>和<contex-param>
  2. 接著容器會建立一個ServletContext,整個WEB項目都能使用這個上下文
  3. 容器將讀到的<context-param>轉化為鍵值對,並交給ServletContext
  4. 容器建立<listener></listener>中的類執行個體,即建立監聽
  5. 在監聽的類中有一個contextInitialized(ServletContextEvent event)初始化方法,在這個方法中可以通過event.getServletContext().getInitParameter("contextConfigLocation") 來得到context-param 設定的值,還有一個contextDestroyed(ServletContextEvent event) 銷毀方法.用於關閉應用前釋放資源
  6. 得到這個context-param的值之後,我們就可以做一些操作了.這時WEB項目還沒有完全啟動完成.

 

 

本文的重點在於明白以上各元素的基本配置,以及它們的載入順序,即跟在web.xml檔案中配置的先後無關,其順序為context-param ->listener -> filter -> servlet.


聯繫我們

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