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="http://JAVA.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
2.display-name
<display-name>test-hwp-web-application</display-name>
定義了web應用的名稱,可以在http://localhost:8080/manager/html中顯示。如下所示:
3.welcome-file-list
<welcome-file-list>
<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>
用來指定對應格式的文件,瀏覽器所處理的方式






