7.1 web.xml 的一般架構

web.xml 文件存儲應用程序配置信息。它不是它的強制性部分,但它被廣泛用於配置 Web 應用程序。

此文件必須位於WEB-INF文件夾中。當 Tomcat 啟動時,它會讀取它的內容並使用它包含的配置。如果文件包含錯誤,Tomcat 也會顯示錯誤。

示例 web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  
  <servlet>
       <servlet-name>HelloWorld</servlet-name>
       <servlet-class>HelloServlet</servlet-class>
   </servlet>
    
  <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>

"HelloWorld"servlet 名稱和 servlet 類的映射在這裡用綠色寫著"HelloServlet"servlet 名稱和 URL 塊的映射是用藍色寫的"HelloWorld""http://localhost/welcome"。因此,它在這裡說,當訪問 /welcome 路徑時,您需要調用 servlet HelloServlet.class

紅色表示需要根據要求提供的文件http://localhost/——這就是所謂的歡迎頁面。如果用戶只需在瀏覽器中鍵入與我們的 Web 應用程序的根對應的名稱,那麼index.html.

7.2 servlet、servlet-映射

一個 servlet 可以為不同 URL 的請求提供服務,因此在 web-xml 中,servlet 及其到 URL 的映射是分開編寫的。首先,我們描述 servlet,給每個 servlet 一個唯一的字符串名稱,然後我們指定每個 servlet 如何映射到哪個 url。

示例 web.xml:



<web-app>
  
  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>com.codegym.RemotingServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <servlet>
    <servlet-name>restapi</servlet-name>
    <servlet-class>com.codegym.RestApiServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>restapi</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>  
 
</web-app>

在此示例中,聲明了兩個 servlet,每個都映射到不同的 url 模板。該 servletRemotingServlet服務所有發往 的請求/remoting/*該 servletRestApiServlet服務所有發往 的請求/api/*。Servlet 也有加載順序——啟動時加載參數。

7.3 Servlet 選項

在 web.xml 的幫助下,可以在初始化期間將參數傳遞給 servlet,它們將通過接口可用ServletConfig。您還可以為整個 Web 應用程序設置參數,它們將通過ServletContext.

示例 web.xml:



<web-app>
  <context-param>
     <description>Server production mode</description>
     <param-name>productionMode</param-name>
     <param-value>false</param-value>
  </context-param>
 
  <context-param>
     <param-name>appPropertiesConfig</param-name>
     <param-value>
        classpath:local-app.properties
        classpath:web-app.properties
     </param-value>
  </context-param>
 
  <servlet>
     <servlet-name>mainservlet</servlet-name>
     <servlet-class>com.codegym.ApplicationServlet</servlet-class>
     <init-param>
        <param-name>application</param-name>
        <param-value>com.codegym.App</param-value>
     </init-param>
     <init-param>
        <param-name>widgetset</param-name>
        <param-value>com.codegym.WidgetSet</param-value>
     </init-param>
     <init-param>
        <param-name>ui</param-name>
        <param-value>com.codegym.AppUI</param-value>
     </init-param>
  </servlet>
</web-app>

以綠色突出顯示的代碼是我們為 設置參數的地方ServletContext。其中有兩個:

  • productionMode價值假
  • appPropertiesConfig帶有兩個字符串的數組:
    • classpath:local-app.properties
    • classpath:web-app.properties

servlet 的參數以藍色表示ApplicationServlet,它們將通過以下方式提供給它ServletConfig

  • application具有價值 com.codegym.App
  • widgetset具有價值 com.codegym.WidgetSet
  • ui具有價值 com.codegym.AppUI

7.4 filter、filter-mapping

Web 應用程序還可能包含特殊的utility servlets - filters. 他們執行各種服務任務:重定向呼叫、檢查授權等。

示例 web.xml:



<web-app>
 
  <servlet>
      <servlet-name>remoting</servlet-name>
      <servlet-class>RemotingServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
      <servlet-name>remoting </servlet-name>
      <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <filter>
      <filter-name>total_filter</filter-name>
      <filter-class>com.javrush.TotalFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>total_filter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

在請求到達 servlet 之前RemotingServlet,它將被過濾器處理TotalFiler。此過濾器配置為攔截所有進入我們的 Web 應用程序的請求。它映射到的 url 模板清楚地暗示了這一點:/*

在接下來的課程中,您將閱讀更多關於 servlet 和過濾器的內容。