溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

web.xml文件內(nèi)容的示例分析

發(fā)布時間:2021-09-17 11:45:20 來源:億速云 閱讀:167 作者:小新 欄目:編程語言

這篇文章主要介紹web.xml文件內(nèi)容的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

web.xml文件詳解

一般的web工程中都會用到web.xml,web.xml主要用來配置,可以方便的開發(fā)web工程。web.xml主要用來配置Filter、Listener、Servlet等。但是要說明的是web.xml并不是必須的,一個web工程可以沒有web.xml文件。

1、WEB工程加載web.xml過程

  經(jīng)過個人測試,WEB工程加載順序與元素節(jié)點在文件中的配置順序無關(guān)。即不會因為 filter 寫在 listener 的前面而會先加載 filter。WEB容器的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet。并且這些元素可以配置在文件中的任意位置。

  加載過程順序如下:

  1. 啟動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結(jié)點。

  2. 緊急著,容創(chuàng)建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。

  3. 容器將<context-param>轉(zhuǎn)換為鍵值對,并交給servletContext。

  4. 容器創(chuàng)建<listener>中的類實例,創(chuàng)建監(jiān)聽器。

2、web.xml文件元素詳解

  1、schema

  web.xml的模式文件是由Sun公司定義的,每個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>

  2、<icon>Web應用圖標

  指出IDE和GUI工具用來表示W(wǎng)eb應用的大圖標和小圖標。

<icon>
    <small-icon>/images/app_small.gif</small-icon>
    <large-icon>/images/app_large.gif</large-icon></icon>

  3、<display-name>Web應用名稱

  提供GUI工具可能會用來標記這個特定的Web應用的一個名稱

<display-name>Tomcat Example</display-name>

  4、<disciption>Web應用描述

  給出于此相關(guān)的說明性文本

<disciption>Tomcat Example servlets and JSP pages.</disciption>

  5、<context-param>上下文參數(shù)

  聲明應用范圍內(nèi)的初始化參數(shù)。它用于向 ServletContext提供鍵值對,即應用程序上下文信息。我們的listener, filter等在初始化時會用到這些上下文中的信息。在servlet里面可以通過getServletContext().getInitParameter("context/param")得到。

<context-param>
    <param-name>ContextParameter</para-name>
    <param-value>test</param-value>
    <description>It is a test parameter.</description></context-param>

  6、<filter>過濾器

  將一個名字與一個實現(xiàn)javaxs.servlet.Filter接口的類相關(guān)聯(lián)。

<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>

  7、<listener>監(jiān)聽器

<listener> 
    <listerner-class>com.listener.SessionListener</listener-class> </listener>

  8、<servlet>

  <servlet></servlet> 用來聲明一個servlet的數(shù)據(jù),主要有以下子元素:

  • <servlet-name></servlet-name> 指定servlet的名稱

  • <servlet-class></servlet-class> 指定servlet的類名稱

  • <jsp-file></jsp-file> 指定web站臺中的某個JSP網(wǎng)頁的完整路徑

  • <init-param></init-param> 用來定義參數(shù),可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數(shù)

  • <load-on-startup></load-on-startup>指定當Web應用啟動時,裝載Servlet的次序。當值為正數(shù)或零時:Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet。當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它。

  • <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素

  • <servlet-name></servlet-name> 指定servlet的名稱

  • <url-pattern></url-pattern> 指定servlet所對應的URL

<!-- 基本配置 --><servlet>
    <servlet-name>snoop</servlet-name>
    <servlet-class>SnoopServlet</servlet-class></servlet><servlet-mapping>
    <servlet-name>snoop</servlet-name>
    <url-pattern>/snoop</url-pattern></servlet-mapping><!-- 高級配置 --><servlet>
    <servlet-name>snoop</servlet-name>
    <servlet-class>SnoopServlet</servlet-class>
    <init-param>
        <param-name>foo</param-name>
        <param-value>bar</param-value>
    </init-param>
    <run-as>
        <description>Security role for anonymous access</description>
        <role-name>tomcat</role-name>
    </run-as></servlet><servlet-mapping>
    <servlet-name>snoop</servlet-name>
    <url-pattern>/snoop</url-pattern></servlet-mapping>

  9、<session-config>會話超時配置

  單位為分鐘。

<session-config>
    <session-timeout>120</session-timeout></session-config>

  10、<mime-mapping>

<mime-mapping>
    <extension>htm</extension>
    <mime-type>text/html</mime-type></mime-mapping>

  11、<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>

  12、<error-page>錯誤頁面

<!-- 1、通過錯誤碼來配置error-page。當系統(tǒng)發(fā)生×××錯誤時,跳轉(zhuǎn)到錯誤處理頁面。 --><error-page>
    <error-code>404</error-code>
    <location>/NotFound.jsp</location></error-page><!-- 2、通過異常的類型配置error-page。當系統(tǒng)發(fā)生java.lang.NullException(即空指針異常)時,跳轉(zhuǎn)到錯誤處理頁面。 --><error-page>
    <exception-type>java.lang.NullException</exception-type>
    <location>/error.jsp</location></error-page>

  13、<jsp-config>設置jsp

  <jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子元素。其中<taglib> 元素在JSP 1.2 時就已經(jīng)存在;而<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 網(wǎng)頁的編碼

  • <include-prelude>:設置 JSP 網(wǎng)頁的抬頭,擴展名為 .jspf

  • <include-coda>:設置 JSP 網(wǎng)頁的結(jié)尾,擴展名為 .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>

  對于Web 應用程式來說,Scriptlet 是個不樂意被見到的東西,因為它會使得HTML 與Java 程式碼交相混雜,對于程式的維護來說相當?shù)穆闊?,必要的時候,可以在web.xml 中加上<script-invalid> 標簽,設定所有的JSP 網(wǎng)頁都不可以使用Scriptlet。

3、Mapping規(guī)則

  當一個請求發(fā)送到servlet容器的時候,容器先會將請求的url減去當前應用上下文的路徑作為servlet的映射url,比如我訪問的是http://localhost/test/aaa.html,我的應用上下文是test,容器會將http://localhost/test去掉,剩下的/aaa.html部分拿來做servlet的映射匹配。這個映射匹配過程是有順序的,而且當有一個servlet匹配成功以后,就不會去理會剩下的servlet了。

  其匹配規(guī)則和順序如下:

  1. 精確路徑匹配。例子:比如servletA 的url-pattern為 /test,servletB的url-pattern為 /* ,這個時候,如果我訪問的url為http://localhost/test ,這個時候容器就會先 進行精確路徑匹配,發(fā)現(xiàn)/test正好被servletA精確匹配,那么就去調(diào)用servletA,也不會去理會其他的servlet了。

  2. 最長路徑匹配。例子:servletA的url-pattern為/test/*,而servletB的url-pattern為/test/a/*,此時訪問http://localhost/test/a時,容器會選擇路徑最長的servlet來匹配,也就是這里的servletB。

  3. 擴展匹配,如果url最后一段包含擴展,容器將會根據(jù)擴展選擇合適的servlet。例子:servletA的url-pattern:*.action

  以”/’開頭和以”/*”結(jié)尾的是用來做路徑映射的。以前綴”*.”開頭的是用來做擴展映射的。所以,為什么定義”/*.action”這樣一個看起來很正常的匹配會錯?因為這個匹配即屬于路徑映射,也屬于擴展映射,導致容器無法判斷。

以上是“web.xml文件內(nèi)容的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI