web.xml文件的做用及基本配置(轉)

其實web.xml就是asp.net的web.config一個道理。html

說明:java

一個web中徹底能夠沒有web.xml文件,也就是說,web.xml文件並非web工程必須的。web

web.xml文件是用來配置:歡迎頁、servlet、filter等的。當你的web工程沒用到這些時,你能夠不用web.xml文件來配置你的web工程。spring

web.xml能作的事情:sql

其實,web.xml的模式(Schema)文件中定義了多少種標籤元素,web.xml中就能夠出現它的模式文件所定義的標籤元素,它就能擁有定義出來的那些功能。數據庫

web.xml的模式文件是由Sun公司定義的,每一個web.xml文件的根元素<web-app>中,都必須標明這個web.xml使用的是哪一個模式文件。如:apache

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://Java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>

並且web.xml的模式文件中定義的標籤並非定死的,模式文件也是能夠改變的,通常來講,隨着web.mxl模式文件的版本升級,裏面定義的功能會愈來愈複雜,也即標籤元素的種類會愈來愈多,但有些是不經常使用的,咱們只需記住一些經常使用的就能夠了。瀏覽器

下面列出web.xml經常使用的標籤元素及這些標籤元素的功能:tomcat

一、指定歡迎頁面,例如:安全

<welcome-file-list>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>

 

上面的例子指定了2個歡迎頁面,顯示時按順序從第一個找起,若是第一個存在,就顯示第一個,後面的不起做用。若是第一個不存在,就找第二個,以此類推。

關於歡迎頁面:

訪問一個網站時,默認看到的第一個頁面就叫歡迎頁,通常狀況下是由首頁來充當歡迎頁的。通常狀況下,咱們會在web.xml中指定歡迎頁。但web.xml並非一個Web的必要文件,沒有web.xml,網站仍然是能夠正常工做的。只不過網站的功能複雜起來後,web.xml的確有很是大用處,因此,默認建立的動態web工程在WEB-INF文件夾下面都有一個web.xml文件。

對於tomcat來講,當你只指定一個web的根名,沒有指定具體頁面,去訪問時一個web時,若是web.xml文件中配置了歡迎頁,那麼就返回指定的那個頁面做爲歡迎頁,而在文中沒有web.xml文件,或雖然有web.xml,但web.xml也沒指定歡迎頁的狀況下,它默認先查找index.html文件,若是找到了,就把index.html做爲歡迎頁還回給瀏覽器。若是沒找到index.html,tomcat就去找index.jsp。找到index.jsp就把它做爲歡迎頁面返回。而若是index.html和index.jsp都沒找到,又沒有用web.xml文件指定歡迎頁面,那此時tomcat就不知道該返回哪一個文件了,它就顯示The requested resource (/XXX) is not available的頁面。其中XXX表示web的根名。但若是你指定了具體頁面,是能夠正常訪問的。

二、命名與定製URL。咱們能夠爲Servlet和JSP文件命名並定製URL,其中定製URL是依賴一命名的,命名必須在定製URL前。下面拿serlet來舉例:

(1)、爲Servlet命名:

<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>net.test.TestServlet</servlet-class>
</servlet>

(2)、爲Servlet定製URL、

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

三、定製初始化參數:能夠定製servlet、JSP、Context的初始化參數,而後能夠再servlet、JSP、Context中獲取這些參數值。下面哪servlet來舉例:

<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>net.test.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Tommy</param-value>
</init-param>
<init-param>
<param-name>E-mail</param-name>
<param-value>Tommy@163.com</param-value>
</init-param>
</servlet>

通過上面的配置,在servlet中可以調用getServletConfig().getInitParameter("param1")得到參數名對應的值。

四、指定錯誤處理頁面,能夠經過「異常類型」或「錯誤碼」來指定錯誤處理頁面。

<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
-----------------------------
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>

五、設置過濾器:好比設置一個編碼過濾器,過濾全部資源

<filter>
<filter-name>XXXCharaSetFilter</filter-name>
<filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XXXCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

六、設置監聽器:

<listener>
<listener-class>net.test.XXXLisenet</listener-class>
</listener>

七、設置會話(Session)過時時間,其中時間以分鐘爲單位,假如設置60分鐘超時:

<session-config>
<session-timeout>60</session-timeout>
</session-config>

除了這些標籤元素以外,還能夠往web.xml中添加那些標籤元素呢,那些標籤元素都能起什麼做用呢?咱們只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,能夠找一些中文教程來看看。

Web.xml加載順序 
1、

一、啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。

二、緊急着,容建立一個ServletContext(servlet上下文),這個web項目的全部部分都將共享這個上下文。

三、容器將<context-param>轉換爲鍵值對,並交給servletContext。

四、容器建立<listener>中的類實例,建立監聽器。

2、 
load-on-startup 元素在web應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。若是它的值是一個負整數或是這個元素不存在,那麼容器會在該servlet被調用的時候,加載這個servlet 。若是值是正整數或零,容器在配置的時候就加載並初始化這個servlet,容器必須保證值小的先被加載。若是值相等,容器能夠自動選擇先加載誰。

在servlet的配置當中,<load-on-startup>5</load-on-startup>的含義是:

標記容器是否在啓動的時候就加載這個servlet。

當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet;

當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載。

正數的值越小,啓動該servlet的優先級越高。

3、 
在項目中總會遇到一些關於加載的優先級問題,近期也一樣遇到過相似的,因此本身查找資料總結了下,下面有些是轉載其餘人的,畢竟人家寫的不錯,本身也就不重複造輪子了,只是略加點了本身的修飾。

首先能夠確定的是,加載順序與它們在 web.xml 文件中的前後順序無關。即不會由於 filter 寫在 listener 的前面而會先加載 filter。

最終得出的結論是:listener ->filter -> servlet 同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。

咱們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是否是應該寫在 listener 配置節前呢?

實際上context-param 配置節可寫在任意位置,所以真正的加載順序爲:context-param ->listener -> filter -> servlet 對於某類配置節而言,與它們出現的順序是有關的。

以 filter 爲例,web.xml 中固然能夠定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這裏必定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping必須出如今 filter 以後,不然當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。

web 容器啓動時初始化每一個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping配置節出現的順序來依次調用 doFilter() 方法的。

servlet 同 filter 相似,此處再也不贅述。

由此,能夠看出,web.xml 的加載順序是:context-param -> listener -> filter-> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。

web.xml文件詳解

1、Web.xml經常使用元素 

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

2、相應元素配置 
一、Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標

<icon> 
<small-icon>/images/app_small.gif</small-icon> 
<large-icon>/images/app_large.gif</large-icon> 
</icon> 

二、Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱

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

三、Web 應用描述:給出於此相關的說明性文本

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

四、上下文參數:聲明應用範圍內的初始化參數。

<context-param> 
<param-name>ContextParameter</para-name> 
<param-value>test</param-value> 
<description>It is a test parameter.</description> 
</context-param> 

在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得

五、過濾器配置:將一個名字與一個實現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> 

六、監聽器配置

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

七、Servlet配置

基本配置

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

元素說明

<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 

八、會話超時配置(單位爲分鐘) 

<session-config>
<session-timeout>120</session-timeout> 
</session-config> 

九、MIME類型配置

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

十、指定歡迎文件頁配置

<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
<welcome-file>index.html</welcome-file> 
<welcome-file>index.htm</welcome-file> 
</welcome-file-list> 

十一、配置錯誤頁面

1、經過錯誤碼來配置error-page

<error-page> 
<error-code>404</error-code> 
<location>/NotFound.jsp</location> 
</error-page> 

上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。

2、經過異常的類型配置error-page

<error-page> 
<exception-type>java.lang.NullException</exception-type> 
<location>/error.jsp</location> 
</error-page> 

上面配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面error.jsp

十二、TLD配置

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

若是MyEclipse一直在報錯,應該把<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> 

1三、資源管理對象配置

<resource-env-ref> 
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name> 
</resource-env-ref>

1四、資源工廠配置

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

1五、安全限制配置

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

1六、登錄驗證配置

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

1七、安全角色:security-role元素給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素的role-name子元素中。 分別地聲明角色可以使高級IDE處理安全信息更爲容易。 

<security-role>
<role-name>tomcat</role-name> 
</security-role> 

1八、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> 

1九、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> 

2一、配置DWR

<servlet> 
<servlet-name>dwr-invoker</servlet-name> 
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>dwr-invoker</servlet-name> 
<url-pattern>/dwr/*</url-pattern> 
</servlet-mapping> 

2二、配置Struts

<display-name>Struts Blank Application</display-name> 
<servlet> 
<servlet-name>action</servlet-name> 
<servlet-class> 
org.apache.struts.action.ActionServlet 
</servlet-class> 
<init-param> 
<param-name>detail</param-name> 
<param-value>2</param-value>
</init-param> 
<init-param> 
<param-name>debug</param-name> 
<param-value>2</param-value> 
</init-param> 
<init-param> 
<param-name>config</param-name> 
<param-value>/WEB-INF/struts-config.xml</param-value> 
</init-param> 
<init-param> 
<param-name>application</param-name> 
<param-value>ApplicationResources</param-value> 
</init-param> 
<load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>action</servlet-name> 
<url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<!-- Struts Tag Library Descriptors --> 
<taglib> 
<taglib-uri>struts-bean</taglib-uri> 
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> 
</taglib> 
<taglib> 
<taglib-uri>struts-html</taglib-uri> 
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> 
</taglib> 
<taglib> 
<taglib-uri>struts-nested</taglib-uri> 
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> 
</taglib> 
<taglib> 
<taglib-uri>struts-logic</taglib-uri> 
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> 
</taglib> 
<taglib> 
<taglib-uri>struts-tiles</taglib-uri> 
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location> 
</taglib>

2三、配置spring(基本上都是在Struts中配置的)

<!-- 指定spring配置文件位置 --> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
<!--加載多個spring配置文件 --> 
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml 
</param-value> 
</context-param> 
<!-- 定義SPRING監聽器,加載spring --> 
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class> 
</listener> 
<listener> 
<listener-class> 
org.springframework.web.context.request.RequestContextListener 
</listener-class> 
</listener>

 

原文:http://blog.csdn.net/shanliangliuxing/article/details/7458492

相關文章
相關標籤/搜索