無敵全web.xml配置文件元素詳解

一、web.xml配置文件常用元素及其意義預覽

<web-app>
 
      <!--定義了WEB應用的名字-->
      <display-name></display-name>
 
      <!--聲明WEB應用的描述信息-->
      <description></description>
 
      <!--context-param元素聲明應用範圍內的初始化參數-->
      <context-param></context-param>
 
      <!--過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯-->
      <filter></filter>
 
      <!--一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯-->
      <filter-mapping></filter-mapping>
 
      <!--servlet API的版本2.3增加了對事件監聽程序的支持,事件監聽程序在建立、修改和刪除會話或servlet環境時得到通知。
          Listener元素指出事件監聽程序類-->
      <listener></listener>
 
      <!--在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。
          Servlet元素就是用來完成此項任務的-->
      <servlet></servlet>
 
      <!--服務器一般爲servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。
          但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。
          在更改缺省URL時,使用servlet-mapping元素-->
      <servlet-mapping></servlet-mapping>
 
      <!--如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節省內存。可通過使用HttpSession的
          setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值-->
      <session-config></session-config>
 
      <!--如果Web應用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證-->
      <mime-mapping></mime-mapping>
 
      <!--指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件-->
      <welcome-file-list></welcome-file-list>
 
      <!--在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面-->
      <error-page></error-page>
 
      <!--對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,
          而不用編輯使用這些文件的JSP頁面-->
      <taglib></taglib>
 
      <!--聲明與資源相關的一個管理對象-->
      <resource-env-ref></resource-env-ref>
 
      <!--聲明一個資源工廠使用的外部資源-->
      <resource-ref></resource-ref>
 
      <!--制定應該保護的URL。它與login-config元素聯合使用-->
      <security-constraint></security-constraint>
 
      <!--指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯合使用-->
      <login-config></login-config>
 
      <!--給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。
          分別地聲明角色可使高級IDE處理安全信息更爲容易-->
      <security-role></security-role>
 
      <!--聲明Web應用的環境項-->
      <env-entry></env-entry>
 
      <!--聲明一個EJB的主目錄的引用-->
      <ejb-ref></ejb-ref>
 
      <!--聲明一個EJB的本地主目錄的應用-->
      <ejb-local-ref></ejb-local-ref>
 
  </web-app> 

二、各個配置元素詳解

1.Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標
 <icon>  
      <small-icon>/images/app_small.gif</small-icon>  
      <large-icon>/images/app_large.gif</large-icon>  
  </icon>

2.Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱
<display-name>Tomcat Example</display-name>

3.Web 應用描述:給出於此相關的說明性文本
<desciption>Tomcat Example servlets and JSP pages.</desciption>

4.上下文參數:聲明應用範圍內的初始化參數
 <context-param>
      <param-name>參數名</para-name>
      <param-value>參數值</param-value>
      <description>參數描述</description>
  </context-param>
在servlet裏面可以通過 getServletContext().getInitParameter(“context/param”)得到

5.過濾器配置:將一個名字與一個實現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>GB2312</param-value>
       </init-param>
   </filter>
   <filter-mapping>
      <filter-name>setCharacterEncoding</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

6.監聽器配置
 <listener>
      <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

7.Servlet配置
  <servlet>
     <servlet-name>servlet名稱</servlet-name>
     <servlet-class>servlet類全路徑</servlet-class>
     <init-param>
         <param-name>參數名</param-name>
         <param-value>參數值</param-value>
     </init-param>
     <run-as>
         <description>Security role for anonymous access</description>
         <role-name>tomcat</role-name>
     </run-as>
   <load-on-startup>指定當Web應用啓動時,裝載Servlet的次序</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>servlet名稱</servlet-name>
   <url-pattern>映射路徑</url-pattern>
 </servlet-mapping>

8.會話超時配置(單位爲分鐘)
 <session-config>
      <session-timeout>120</session-timeout>
  </session-config>

9.MIME類型配置
 <error-page>
       <exception-type>java.lang.NullException</exception-type>
       <location>/error.jsp</location>
 </error-page>


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

10.指定歡迎文件頁配置

  <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.配置錯誤頁面
(1).通過錯誤碼來配置error-page
 <!--配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp-->
 <error-page>
       <error-code>404</error-code>
       <location>/NotFound.jsp</location>
  </error-page>
(2).通過異常的類型配置error-page
1 <!--配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面error.jsp-->
 <error-page>
       <exception-type>java.lang.NullException</exception-type>
       <location>/error.jsp</location>
 </error-page>
 <resource-ref>
      <res-ref-name>mail/Session</res-ref-name>
      <res-type>javax.mail.Session</res-type>
      <res-auth>Container</res-auth>
 </resource-ref>



12.TLD配置
 <taglib>
      <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
      <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
  </taglib>
如果開發工具一直在報錯,應該把<taglib> 放到 <jsp-config>中
 <jsp-config>
      <taglib>
          <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
          <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
      </taglib>
  </jsp-config>
 

13.資源管理對象配置
 <resource-env-ref>
      <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
  </resource-env-ref>

14.資源工廠配置
 <resource-ref>
      <res-ref-name>mail/Session</res-ref-name>
      <res-type>javax.mail.Session</res-type>
      <res-auth>Container</res-auth>
 </resource-ref>
   配置數據庫連接池就可在此配置

  <resource-ref>
      <description>JNDI JDBC DataSource of shop</description>
      <res-ref-name>jdbc/sample_db</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
 
15.安全限制配置
  <security-constraint>
       <display-name>Example Security Constraint</display-name>
       <web-resource-collection>
           <web-resource-name>Protected Area</web-resource-name>
           <url-pattern>/jsp/security/protected/*</url-pattern>
           <http-method>DELETE</http-method>
           <http-method>GET</http-method>
           <http-method>POST</http-method>
           <http-method>PUT</http-method>
      </web-resource-collection>
      <auth-constraint>
          <role-name>tomcat</role-name>
          <role-name>role1</role-name>
      </auth-constraint>
 </security-constraint>

16.登陸驗證配置
  <login-config>
      <auth-method>FORM</auth-method>
      <realm-name>Example-Based Authentiation Area</realm-name>
      <form-login-config>
          <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
          <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
      </form-login-config>
  </login-config>

17.安全角色:security-role元素給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級IDE處理安全信息更爲容易。
 <security-role>
      <role-name>tomcat</role-name>
  </security-role>

18.Web環境參數:env-entry元素聲明Web應用的環境項
 <env-entry>
      <env-entry-name>minExemptions</env-entry-name>
      <env-entry-value>1</env-entry-value>
      <env-entry-type>java.lang.Integer</env-entry-type>
 </env-entry>
 

19.EJB 聲明
 <ejb-ref>
      <description>Example EJB reference</decription>
      <ejb-ref-name>ejb/Account</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <home>com.mycompany.mypackage.AccountHome</home>
      <remote>com.mycompany.mypackage.Account</remote>
  </ejb-ref>
 

20.本地EJB聲明
  <ejb-local-ref>
      <description>Example Loacal EJB reference</decription>
      <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
      <local>com.mycompany.mypackage.ProcessOrder</local>
  </ejb-local-ref>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章