史上最全web.xml設定檔元素詳解

來源:互聯網
上載者:User

標籤:sql   利用   預覽   local   標記   http   數值   delete   prot   

一、web.xml設定檔常用元素及其意義預覽

 1 <web-app> 2  3      <!--定義了WEB應用的名字--> 4      <display-name></display-name> 5  6      <!--聲明WEB應用的描述資訊--> 7      <description></description> 8  9      <!--context-param元素宣告應用範圍內的初始化參數-->10      <context-param></context-param>11 12      <!--過濾器元素將一個名字與一個實現javax.servlet.Filter介面的類相關聯-->13      <filter></filter>14 15      <!--一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯-->16      <filter-mapping></filter-mapping>17 18      <!--servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知。19          Listener元素指出事件監聽程式類-->20      <listener></listener>21 22      <!--在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。23          Servlet元素就是用來完成此項任務的-->24      <servlet></servlet>25 26      <!--伺服器一般為servlet提供一個預設的URL:http://host/webAppPrefix/servlet/ServletName。27          但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。28          在更改預設URL時,使用servlet-mapping元素-->29      <servlet-mapping></servlet-mapping>30 31      <!--如果某個會話在一定時間內未被訪問,伺服器可以拋棄它以節省記憶體。可通過使用HttpSession的32          setMaxInactiveInterval方法明確設定單個會話對象的逾時值,或者可利用session-config元素制定預設逾時值-->33      <session-config></session-config>34 35      <!--如果Web應用具有想到特殊的檔案,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證-->36      <mime-mapping></mime-mapping>37 38      <!--指示伺服器在收到引用一個目錄名而不是檔案名稱的URL時,使用哪個檔案-->39      <welcome-file-list></welcome-file-list>40 41      <!--在返回特定HTTP狀態碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面-->42      <error-page></error-page>43 44      <!--對標記庫描述符檔案(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD檔案的位置,45          而不用編輯使用這些檔案的JSP頁面-->46      <taglib></taglib>47 48      <!--聲明與資源相關的一個管理對象-->49      <resource-env-ref></resource-env-ref>50 51      <!--聲明一個資源工廠使用的外部資源-->52      <resource-ref></resource-ref>53 54      <!--制定應該保護的URL。它與login-config元素聯合使用-->55      <security-constraint></security-constraint>56 57      <!--指定伺服器應該怎樣給試圖訪問受保護頁面的使用者授權。它與sercurity-constraint元素聯合使用-->58      <login-config></login-config>59 60      <!--給出資訊安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子項目中。61          分別地聲明角色可使進階IDE處理安全資訊更為容易-->62      <security-role></security-role>63 64      <!--聲明Web應用的環境項-->65      <env-entry></env-entry>66 67      <!--聲明一個EJB的主目錄的引用-->68      <ejb-ref></ejb-ref>69 70      <!--聲明一個EJB的本地主目錄的應用-->71      <ejb-local-ref></ejb-local-ref>72 73  </web-app> 

二、各個配置元素詳解

1.Web應用表徵圖:指出IDE和GUI工具用來表示Web應用的大表徵圖和小表徵圖

1 <icon>  2      <small-icon>/images/app_small.gif</small-icon>  3      <large-icon>/images/app_large.gif</large-icon>  4  </icon>

 

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

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

 

3.Web 應用程式描述:給出於此相關的說明性文本

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

 

4.上下文參數:聲明應用範圍內的初始化參數

1 <context-param>2      <param-name>參數名</para-name>3      <param-value>參數值</param-value>4      <description>參數描述</description>5  </context-param>

在servlet裡面可以通過 getServletContext().getInitParameter(“context/param”)得到

 

5.過濾器配置:將一個名字與一個實現javaxs.servlet.Filter介面的類相關聯

 1 <filter> 2      <filter-name>setCharacterEncoding</filter-name> 3      <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> 4      <init-param> 5          <param-name>encoding</param-name> 6          <param-value>GB2312</param-value> 7      </init-param> 8  </filter> 9  <filter-mapping>10      <filter-name>setCharacterEncoding</filter-name>11      <url-pattern>/*</url-pattern>12  </filter-mapping>

 

6.監聽器配置

1 <listener>2      <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>3  </listener>

 

7.Servlet配置

 1 <servlet> 2    <servlet-name>servlet名稱</servlet-name> 3    <servlet-class>servlet類全路徑</servlet-class> 4    <init-param> 5        <param-name>參數名</param-name> 6        <param-value>參數值</param-value> 7    </init-param> 8    <run-as> 9        <description>Security role for anonymous access</description>10        <role-name>tomcat</role-name>11    </run-as>12   <load-on-startup>指定當Web應用啟動時,裝載Servlet的次序</load-on-startup>13 </servlet>14 <servlet-mapping>15   <servlet-name>servlet名稱</servlet-name>16   <url-pattern>映射路徑</url-pattern>17 </servlet-mapping>

 

8.會話逾時配置(單位為分鐘)

1 <session-config>2      <session-timeout>120</session-timeout>3  </session-config>

 

9.MIME類型配置

1 <mime-mapping>2      <extension>htm</extension>3      <mime-type>text/html</mime-type>4  </mime-mapping>

 

10.指定歡迎檔案頁配置

1  <welcome-file-list>2      <welcome-file>index.jsp</welcome-file>3      <welcome-file>index.html</welcome-file>4      <welcome-file>index.htm</welcome-file>5  </welcome-file-list>

 

11.配置錯誤頁面

  (1).通過錯誤碼來配置error-page

1 <!--配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp-->2 <error-page>3       <error-code>404</error-code>4       <location>/NotFound.jsp</location>5  </error-page>

  (2).通過異常的類型配置error-page

1 <!--配置了當系統發生java.lang.NullException(即null 指標異常)時,跳轉到錯誤處理頁面error.jsp-->2 <error-page>3       <exception-type>java.lang.NullException</exception-type>4       <location>/error.jsp</location>5 </error-page>

 

12.TLD配置

1 <taglib>2      <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>3      <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>4  </taglib>

如果開發工具一直在報錯,應該把<taglib> 放到 <jsp-config>中

1 <jsp-config>2      <taglib>3          <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>4          <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>5      </taglib>6  </jsp-config>

 

13.資源管理對象配置

1 <resource-env-ref>2      <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>3  </resource-env-ref>

 

14.資源工廠配置

1 <resource-ref>2      <res-ref-name>mail/Session</res-ref-name>3      <res-type>javax.mail.Session</res-type>4      <res-auth>Container</res-auth>5 </resource-ref>

  設定資料庫串連池就可在此配置

1  <resource-ref>2      <description>JNDI JDBC DataSource of shop</description>3      <res-ref-name>jdbc/sample_db</res-ref-name>4      <res-type>javax.sql.DataSource</res-type>5      <res-auth>Container</res-auth>6  </resource-ref>

 

15.安全限制配置

 1 <security-constraint> 2      <display-name>Example Security Constraint</display-name> 3      <web-resource-collection> 4          <web-resource-name>Protected Area</web-resource-name> 5          <url-pattern>/jsp/security/protected/*</url-pattern> 6          <http-method>DELETE</http-method> 7          <http-method>GET</http-method> 8          <http-method>POST</http-method> 9          <http-method>PUT</http-method>10      </web-resource-collection>11      <auth-constraint>12          <role-name>tomcat</role-name>13          <role-name>role1</role-name>14      </auth-constraint>15 </security-constraint>

 

16.登陸驗證配置

1  <login-config>2      <auth-method>FORM</auth-method>3      <realm-name>Example-Based Authentiation Area</realm-name>4      <form-login-config>5          <form-login-page>/jsp/security/protected/login.jsp</form-login-page>6          <form-error-page>/jsp/security/protected/error.jsp</form-error-page>7      </form-login-config>8  </login-config>

 

17.資訊安全角色:security-role元素給出資訊安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子項目中。
分別地聲明角色可使進階IDE處理安全資訊更為容易。

1 <security-role>2      <role-name>tomcat</role-name>3  </security-role>

 

18.Web環境參數:env-entry元素宣告Web應用的環境項

1 <env-entry>2      <env-entry-name>minExemptions</env-entry-name>3      <env-entry-value>1</env-entry-value>4      <env-entry-type>java.lang.Integer</env-entry-type>5 </env-entry>

 

19.EJB 聲明

1 <ejb-ref>2      <description>Example EJB reference</decription>3      <ejb-ref-name>ejb/Account</ejb-ref-name>4      <ejb-ref-type>Entity</ejb-ref-type>5      <home>com.mycompany.mypackage.AccountHome</home>6      <remote>com.mycompany.mypackage.Account</remote>7  </ejb-ref>

 

20.本地EJB聲明

1  <ejb-local-ref>2      <description>Example Loacal EJB reference</decription>3      <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>4      <ejb-ref-type>Session</ejb-ref-type>5      <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>6      <local>com.mycompany.mypackage.ProcessOrder</local>7  </ejb-local-ref>

 

三、總結

  以上就是常用的web.xml中元素的配置以及作用了,歡迎提出異議和不適當的地方共同學習。

史上最全web.xml設定檔元素詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.