1. 程式人生 > 實用技巧 >web.xml檔案詳解

web.xml檔案詳解

web.xml檔案詳解

前言:一般的web工程中都會用到web.xml,web.xml主要用來配置,可以方便的開發web工程。web.xml主要用來配置Filter、Listener、Servlet等。但是要說明的是web.xml並不是必須的,一個web工程可以沒有web.xml檔案。

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

經過個人測試,WEB工程載入順序與元素節點在檔案中的配置順序無關。即不會因為 filter 寫在 listener 的前面而會先載入 filter。WEB容器的載入順序是:ServletContext -> context-param -> listener -> filter -> servlet。並且這些元素可以配置在檔案中的任意位置。

載入過程順序如下:

  • 啟動一個WEB專案的時候,WEB容器會去讀取它的配置檔案web.xml,讀取<listener>和<context-param>兩個結點。

  • 緊急著,容建立一個ServletContext(servlet上下文),這個web專案的所有部分都將共享這個上下文。

  • 容器將<context-param>轉換為鍵值對,並交給servletContext。

  • 容器建立<listener>中的類例項,建立監聽器。

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工具用來表示Web應用的大圖示和小圖示。

<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應用描述

給出於此相關的說明性文字

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

5、<context-param>上下文引數

宣告應用範圍內的初始化引數。它用於向 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>過濾器

將一個名字與一個實現javaxs.servlet.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>

7、<listener>監聽器

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

8、<servlet>

<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>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。當系統發生×××錯誤時,跳轉到錯誤處理頁面。 -->
<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>

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

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



作者:ProZoom
連結:https://www.jianshu.com/p/35c414c06cd9
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。