轉自:
http://www.java265.com/JavaProblem/202108/635.html
在java web開發,我們經常看見WEB-INF下有一個web.xml檔案,那么這個檔案起什么作用呢?
下文將一一道來,如下所示:
web.xml的作用
web.xml檔案的作用用于配置Filter、Listener、Servlet等功能
web.xml注意事項:
web.xml在一個動態web專案中,并不是必須的
即:一個Web工程,可以沒有web.xml檔案
WEB工程加載web.xml說明
web工程加載順序注意事項:
web.xml中元素加載的順序同節點的順序無關
----------------------加載順序-----------------------------------
WEB容器(tomcat)的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet
例: 當我們啟動一個web容器(tomcat)
它首先會去讀取web.xml檔案
然后讀取<創建>和<context-param>節點
然后ServletContext(servlet背景關系)
然后創建監聽器
web.xml檔案元素詳解
schema
設定web.xml使用的模式檔案,模式檔案的內容將放入web-app中
在web.xml配置中,我們必須設定使用的具體模式檔案
并放入<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>
<icon>Web應用圖示
用于設定IDE及GUI工具,用于表示web應用的大圖示及小圖示 <icon> <small-icon>/images/app_small.gif</small-icon> <large-icon>/images/app_large.gif</large-icon> </icon>
<display-name>用于設定Web應用名稱
為GUI工具設定一個應用程式的名稱
例:
<display-name>spring MVC</display-name>
<disciption>Web應用描述
對web應用程式進行相應的描述
例:
<disciption>這是一個spring mvc的樣例程式</disciption>
<context-param>背景關系引數
為ServletContext提供初始化引數
常采用鍵值對模式
例:
<context-param>
<param-name>ContextParameter</para-name>
<param-value>引數值</param-value>
<description>引數值描述</description>
</context-param>
<filter>過濾器
為應用程式設定過濾器
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>監聽器
<listener>
<listerner-class>com.listener.SessionListener</listener-class>
</listener>
<servlet>
servlet:web專案的核心,用于處理前臺的請求
<servlet></servlet>
servlet包含以下元素:
<servlet-name></servlet-name> 指定servlet的名稱
<servlet-class></servlet-class> 指定servlet的類名稱
<jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑
<init-param></init-param> 用來定義引數,可有多個init-param,在servlet類中通過getInitParamenter(String name)方法訪問初始化引數
<load-on-startup></load-on-startup>指定當Web應用啟動時,裝載Servlet的次序,當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet,當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它,
<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素
<servlet-name></servlet-name> 指定servlet的名稱
<url-pattern></url-pattern> 指定servlet所對應的URL
<!-- 基本配置 -->
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>s1Servelet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/webPageUrl</url-pattern>
</servlet-mapping>
<!-- 高級配置 -->
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>s2Servlet</servlet-class>
<init-param>
<param-name>par1</param-name>
<param-value>vale1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/web2</url-pattern>
</servlet-mapping>
<session-config>會話超時配置
單位:分鐘
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<mime-mapping>
mime映射設定
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<welcome-file-list>歡迎頁設定
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<error-page>錯誤頁配置
<!-- 1、通過錯誤碼來配置error-page,當系統發生×××錯誤時,跳轉到錯誤處理頁面, -->
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
<!-- 2、通過例外的型別配置error-page,當系統發生java.lang.NullException(即空指標例外)時,跳轉到錯誤處理頁面, -->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
<jsp-config>設定jsp
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子元素
其中<taglib> 元素在JSP 1.2 時就已經存在
而<jsp-property-group> 是JSP 2.0 新增的元素
<jsp-property-group> 元素主要有八個子元素,它們分別為:
<description>:設定的說明
<display-name>:設定名稱
<url-pattern>:設定值所影響的范圍,如: /CH2 或 /*.jsp
<el-ignored>:若為 true,表示不支持 EL 語法
<scripting-invalid>:若為 true,表示不支持 <% scripting %>語法
<page-encoding>:設定 JSP 網頁的編碼
<include-prelude>:設定 JSP 網頁的抬頭,擴展名為 .jspf
<include-coda>:設定 JSP 網頁的結尾,擴展名為 .jspf
<jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
<description>Special property group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>/jsp/* </url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GB2312</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/include/prelude.jspf</include-prelude>
<include-coda>/include/coda.jspf</include-coda>
</jsp-property-group>
</jsp-config>
Mapping規則
mapping轉發規則:
當一個請求達到servlet容器時,
此時請求的url會轉換為uri,
然后同web.xml中的serverMapping進行匹配
匹配成功后,進入相應的servlet對應的class進行處理
servermapping匹配規則按照以下順序匹配:
- 精確路徑匹配:servletA 的url-pattern為 /test,servletB的url-pattern為 /* ,當訪問的url為http://localhost/test 會匹配servletA對應的Class
- 最長路徑匹配:servletA的url-pattern為/test/*,而servletB的url-pattern為/test/a/*,當訪問http://localhost/test/a時,會匹配servletB對應的Class
- 擴展路徑匹配:servletA的url-pattern:*.do,當我們訪問http://local/test.do時,則會匹配servletA對應的Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499697.html
標籤:其他
