首頁>技術>

web.xml簡介

web.xml是web應用的基礎配置檔案,但又不是必須的。web.xml主要用來配置Filter、Listener、Servlet等。我們常用的框架多數都要通過web.xml檔案進行配置後才能引入並使用。

載入web.xml過程

(1)啟動一個應用,web容器會讀取它的配置檔案web.xml,讀取<listener>和<context-param>兩個結點

(2)建立一個ServletContext,這個web專案的所有部分都將共享這個上下文

(3)容器將<context-param>轉換為鍵值對,並交給ServletContext

(4)容器建立<listener>中的類例項,根據配置的class類路徑<listener-class>來建立監聽,在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,啟動Web應用時,系統呼叫Listener的該方法

(5)容器會讀取<filter></filter>,根據指定的類路徑來例項化過濾器

(6)以上是應用完全啟動起來的時候就已經完成的工作。如果系統中有Servlet,則Servlet是在第一次發起請求的時候被例項化的,而且一般不會被容器銷燬,它可以服務於多個使用者的請求。

(7)總的來說,web.xml的載入順序是:ServletContext -> context-param -> listener -> filter -> servlet,不會因為元素在檔案中的前後順序而改變。如果web.xml中出現了相同的元素,則按照在配置檔案中出現的先後順序來載入。

web.xml檔案元素1.web-app

部署描述符的根元素是<web-app>,寫法如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"     xmlns="/file/2019/12/19/20191219173521_28375.jpg     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee         /file/2019/12/19/20191219174030_28407.jpg    <welcome-file>index1.jsp</welcome-file>    <welcome-file>index2.jsp</welcome-file></welcome-file-list>

該標籤專門是配置應用首頁的,從根目錄開始依次查詢。

4.context-param
<context-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/web-context.xml</param-value>  </context-param><context-param>     <param-name>log4jConfigLocation</param-name>     <param-value>classpath:log4j.properties</param-value></context-param>

該標籤專門是配置應用範圍內的初始化引數。

5.filter
<filter>       <filter-name>hiddenHttpMethodFilter</filter-name>       <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  </filter>  <filter-mapping>       <filter-name>hiddenHttpMethodFilter</filter-name>       <url-pattern>/*</url-pattern>  </filter-mapping>

該標籤專門配置filter過濾器,filter-class指定使用的過濾器,此處我使用的HiddenHttpMethodFilter過濾器就是將頁面中的請求轉化為Rest風格的Http請求。url-pattern主要是過濾的表示式。

6.servlet
<servlet>    <servlet-name>grhc</servlet-name>    <servlet-class>com.hlw.ssm.web.servlet.MyDispatcherServlet</servlet-class>    <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>/WEB-INF/web-context.xml</param-value>      </init-param>    <load-on-startup>1</load-on-startup>    <async-supported>true</async-supported></servlet><servlet-mapping>    <servlet-name>grhc</servlet-name>    <url-pattern>/*</url-pattern></servlet-mapping>

servlet-name:指定servlet應用的名稱。init-param:指的是初始化引數,包含引數名和引數值。load-on-startup:意思是在容器啟動的時候是否就載入servlet,即初始化servlet並且執行init方法。該值大於0就代表容器啟動的時候就載入servlet,而小於0就代表使用servlet時才載入。

7.listener
<listener>    <listener-class>        org.springframework.web.context.ContextLoaderListener    </listener-class></listener>

監聽器用於監聽HttpSession、ServletRequest等域物件的建立與銷燬事件。此處用得spring監聽器ContextLoaderListener目得是在容器啟動的時候,自動載入ApplicationContext配置資訊。

8.session-config
<session-config>    <session-timeout>30</session-timeout></session-config>

該標籤專門配置session失效的時間,也可以通過程式碼request.getSession.setMaxInactiveInterval來實現的。

9.error-page
<error-page>      <error-code>404</error-code>      <location>/error/404</location> </error-page> <error-page>      <error-code>500</error-code>      <location>/error/500</location> </error-page><error-page>       <exception-type>java.lang.Exception</exception-type>       <location>/error/500</location> </error-page>

意思就是Http的狀態碼返回404,500錯誤,就跳轉到指定的location頁面。exception-type就是指web應用丟擲了指定的異常就跳轉到指定的location頁面。

10.mime-mapping
<mime-mapping>    <extension>pdf</extension>    <mime-type>application/pdf</mime-type></mime-mapping>

用來指定對應格式的檔案,瀏覽器所處理的方式

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • PHP框架之Laravel基礎知識最全總結,還不快收藏