web.xml 檔案配置01

來源:互聯網
上載者:User

標籤:log   lex   模式   parameter   包含   span   更改   session   gets   

web.xml 檔案配置01   前言:一般的web工程中都會用到web.xml,方便開發web工程。web.xml主要用來配置Filter、Listener、Servlet等。但是要說明的是web.xml並不是必須的,一個web工程可以沒有web.xml檔案。   1、定義頭和根項目    部署描述符檔案就像所有XML檔案一樣,必須以一個XML頭開始。這個頭聲明可以使用的XML版本並給出檔案的字元編碼。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、web.xml檔案元素詳解  下面的列表給出了所有可直接出現在web-app元素內的合法元素的次序。例如,此列表說明servlet元素必須出現在所有servlet-mapping元素之 前。請注意,所有這些元素都是可選的。    (1)  <icon>Web應用表徵圖            指出IDE和GUI工具用來表示Web應用的應用的大表徵圖和小表徵圖的位置            <icon>        <small-icon>/images/app_small.gif</small-icon>        <large-icon>/images/app_large.gif</large-icon>      </icon>    (2) <display-name> Web應用程式名稱      提供GUI工具可能會用來標記這個特定的Web應用的一個名稱      <display>my webapp</display>          (3) <description> Web應用說明
      給出於此相關的說明性文本      <description> This is my first webapp</description>     (4) <context-param>上下文參數      聲明應用範圍內的初始化參數。它用於向 ServletContext提供索引值對,即應用程式上下文資訊。我們的listener, filter等在初始化時會用到這些上下文中的資訊。在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到。     <context-param>      <param-name>ContextParameter</param-name>      <param-value>test</param-value>      <description>It is a test parameter.</description>    </context-param>     (5) <filter> and <filter-mapping> 過濾器      <filter> 將一個名字與一個實現javaxs.servlet.Filter介面的類相關聯      <filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。       <filter>        <filter-name>myTestFilter</filter-name>        <filter-class>com.myTest.MyTestFilter</filter-class>      </filter>      <filter-mapping>        <filter-name>myTestFilter<filter-name>        <url-pattern>/*</url-pattern>      </filter-mapping>     (6) <listener> 監聽器        將一個名字與一個實現了相應Listener介面的類相關聯        servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知。Listener元素指出事件監聽程式類。        <listener>                <listener-class>com.listener.SessionListener</listener-class>      </listener>     (7) <servlet> and <servlet-mapping> 定製URL        在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。        伺服器一般為servlet提供一個預設的URL:"http://host/webAppPrefix/servlet/ServletName">http://host/webAppPrefix/servlet/ServletName</a>。但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。在更改預設URL時,使用servlet-mapping元素。
            <servlet></servlet> 用來聲明一個servlet的資料,主要有以下子項目:

      a. <servlet-name></servlet-name> 指定servlet的名稱

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

      c. <jsp-file></jsp-file> 指定web月台中的某個JSP網頁的完整路徑

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

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

      f. <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子項目

      g. <servlet-name></servlet-name> 指定servlet的名稱

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

      <servlet>

        <servlet-name>testServlet</servlet-name>

        <servlet-class>com.testservlet.TestServlet</servlet-class>

      </servlet>

      <servlet-mapping>

         <servlet-name>testServlet</servlet-name>

          <url-pattern>/test/testServlet</url-pattern>

      </servlet-mapping>

     (8) <session-config> 會話逾時配置 (單位:分鐘)        如果某個會話在一定時間內未被訪問,伺服器可以拋棄它以節省記憶體。可通過使用HttpSession的setMaxInactiveInterval方法 明確設定單個會話對象的逾時值,或者可利用session-config元素制定預設逾時值。       <session-config>        <session-timeout>120</session-timeout>      </session-config>          (9): <mime-mapping>         如果Web應用具有想到特殊的檔案,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證        <mime-mapping>        <extension>htm</extension>        <mime-type>text/html</mime-type>      </mime-mapping>      (10)  <welcome-file-list>歡迎檔案頁        指示伺服器在收到引用一個目錄名而不是檔案名稱的URL時,使用哪個文
        <welcome-file-list>          <welcome-file>index.jsp</welcome-file>          <welcome-file>index.html</welcome-file>          <welcome-file>index.htm</welcome-file>        </welcome-file-list>      (11) <error-page> 錯誤頁面        使得在返回特定HTTP狀態碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。        <error-page>          <error-code>404</error-code>          <location>/NotFound.jsp</location>        </error-page>                <error-page>          <error-type>java.lang.NullException</error-type>          <location>/error.jsp</location>        </error-page>      (12) <jsp-config>設定jsp

      <jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子項目。其中<taglib> 元素在JSP 1.2 時就已經存在;而<jsp-property-group> 是JSP 2.0 新增的元素。      <taglib>元素對標記庫描述符檔案(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD檔案的位置,而不用編輯使用這些檔案的JSP頁面。  <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  

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

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

      (13) <resource-env-ref>        指定對管理對象的servlet引用的聲明,該對象與servlet環境中的資源相關聯           <resource-env-ref>          <resource-env-ref-name>資源名</resource-env-ref-name>          <resource-env-ref-type>尋找資源時返回的資源類名</resource-env-ref-type>          </resource-env-ref> 

        <resource-env-ref-name>:資源的名稱 相對於Java:comp/env >>context        <resource-env-ref-type>:當web應用尋找該資源的時候,返回的Java類名的全稱     (14) <resource-ref>        聲明一個資源工廠使用的外部資源      (15) <security-constraint>        制定應該保護的URL。它與login-config元素聯合使用       (16) <login-config>        用login-config元素來指定伺服器應該怎樣給試圖訪問受保護頁面的使用者授權。它與sercurity-constraint元素聯合使用          (17) <security-role>        security-role元素給出資訊安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子項目中。分別地聲明角色可使進階IDE處理安全資訊更為容易。       (18) <env-entry>        env-entry元素宣告Web應用的環境項           (19) <ejb-ref>        ejb-ref元素宣告一個EJB的主目錄的引用            (20)  <

ejb-local-ref

>        ejb-local-ref元素宣告一個EJB的本地主目錄的應用             

web.xml 檔案配置01

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.