一、web.xml文件在每一個web工程不是必需要有的:
web.xml文件是用來初始化配置信息:好比Welcome頁面、servlet、servlet-mapping、filter、listener、啓動加載級別等。當web工程沒用到這些時,你能夠不用web.xml文件來配置你的Application。java
二、web.xml定義的內容及加載的順序
2.1web.xml定義的內容
.站臺的名稱和說明 即:定義了WEB應用的名字
.針對環境參數(Context)作初始化工做
.Servlet的名稱和映射
.Session的設定
.Tag library的對映
.JSP網頁設定
.Mime Type處理
.錯誤處理
.利用JDNI取得站臺資源
備註:每一個xml文件都有定義它書寫規則的Schema文件,也就是說javaEE的定義web.xml所對應的xml Schema文件中定義了多少種標籤元素,web.xml中就能夠出現它所定義的標籤元素,也就具有哪些特定的功能。web.xml的模式文件是由Sun 公司定義的,每一個web.xml文件的根元素爲<web-app>中,必須標明這個web.xml使用的是哪一個模式文件。如:
<?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模式文件的版本升級,裏面定義的功能會愈來愈複雜,標籤元素的種類確定也會愈來愈多,但有些不是很經常使用的,咱們只需記住一些經常使用的並知道怎麼配置就能夠了。
2.1web.xml中內容的加載順序
容器啓動時,web項目中文件的加載順序:
1)啓動一個WEB項目,WEB容器會先去讀取它的配置文件web.xml,讀取<context-param>和<listener>兩個節點
2)接着,容器建立一個ServletContext(servlet上下文),這個web項目的全部部分都將共享這個上下文
3)容器將<context-param>轉換爲鍵值對,並交給servletContext。
4)容器建立<listener>中的類實例,建立監聽器。
補充說明:
1)加載順序與它們在 web.xml 文件中的前後順序無關。即不會由於 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:listener –> filter –> servlet
2)同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。咱們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是否是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,所以真正的加載順序爲:context-param –> listener –> filter –> servlet
3)對於某類配置節而言,與它們出現的順序是有關的。以 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
三、web.xml文件中相應元素配置理解
3.1)welcome-file-list
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>
PS:指定了2個歡迎頁面,顯示時按順序從第一個找起,若是第一個存在,就顯示第一個,後面的不起做用。若是第一個不存在,就找第二個,以此類推。
3.2)servlet命名和URL定製
1)爲Servlet命名:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
</servlet>
2)爲Servlet定製URL
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.3)定製初始化參數:能夠定製servlet、JSP、Context的初始化參數,而後能夠再servlet、JSP、Context中獲取這些參數值。
下面用servlet來舉例:
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Daniel</param-value>
<load-on-startup>10</load-on-startup>
</init-param>
</servlet>
load-on-startup 標記容器是否在啓動的時候就加載這個servlet。當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet;當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載。正數的值越小,啓動該servlet的優先級越高
通過上面的配置,在servlet中可以調用getServletConfig().getInitParameter("userName")得到參數名對應的值。
3.4)指定錯誤處理頁面,error-page元素包含三個子元素error-code,exception-type和location.將錯誤代碼(Error Code)或異常(Exception)的種類對應
到web站臺資源路徑.
<error-page>
<error-code>錯誤代碼</error-code> HTTP Error code,例如: 404
<exception-type>Exception</exception-type>一個完整名稱的Java異常類型
<location>/路徑</location>在web站臺內的相關資源路徑
</error-page>
3.5)設置過濾器:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯 好比設置一個編碼過濾器,過濾全部資源 spring
<filter> <filter-name>EncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> <!-- <param-value>GBK</param-value>--> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.6)設置監聽器:
<listener>
<listener-class>net.test.XXXLisenet</listener-class>
</listener>
3.7)設置會話(Session)過時時間,其中時間以分鐘爲單位,假如設置60分鐘超時:
<session-config>
<session-timeout>60</session-timeout>
</session-config>
3.8)web應用的名稱及描述
<display-name>站臺名稱</display-name>定義站臺的名稱. 即web應用的名稱
<description>站臺描述</discription>對站臺作出描述 即web應用的描述
3.9)web站臺中的大小圖標
icon元素包含small-icon和large-icon兩個子元素.用來指定web站臺中小圖標和大圖標的路徑.
<small-icon>/路徑/smallicon.gif</small-icon> small-icon元素應指向web站臺中某個小圖標的路徑,大小爲16 X 16 pixel,可是圖象文件必須爲GIF或JPEG格 式,擴展名必須爲:.gif或.jpg.
<large-icon>/路徑/largeicon-jpg</large-icon>large-icon元素應指向web站臺中某個大圖表路徑,大小爲32 X 32 pixel,可是圖象文件必須爲GIF或JPEG的格 式,擴展名必須爲; gif或jpg.
範例:
<display-name>Develop Example</display-name>
<description>JSP 2.0 Tech Book's Examples</description>
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gir</large-icon>
</icon>
3.10)web站臺是否可分佈式處理
<distributable>
distributable 元素爲空標籤,它的存在與否能夠指定站臺是否可分佈式處理.若是web.xml中出現這個元素,則表明站臺在開發時已經被設計爲能在多個JSP Container 之間分散執行
<distributable/>
3.11)<context-param>:聲明應用範圍內的初始化參數
context-param 元素用來設定web站臺的環境參數(context),它包含兩個子元素:param-name和param-value.
<context-param>
<param-name>參數名稱</param-name>設定Context名稱
<param-value>值</param-value>設定Context名稱的值
</context-param>
範例:
<context-param>
<param-name>param_name</param-name>
<param-value>param_value</param-value>
</context-param>
備註:
此所設定的參數,在JSP網頁中可使用下列方法來取得:${initParam.param_name}
若在Servlet可使用下列方法來得到:String param_name=getServletContext().getInitParamter("param_name");
3.12)過濾器
filter元素用來聲明filter的相關設定.filter元素除了下面介紹的的子元素以外,還包括<servlet>、<icon>、<display-name>、<description>、<init-param>其用途同樣
<filter>
<filter-name>Filter的名稱</filter-name>定義Filter的名稱.
<filter-class>Filter的類名稱</filter-class>定義Filter的類名稱.例如:com.foo.hello
</filter>
範例:
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
3.13)過濾器映射器
filter-mapping 元素的兩個主要子元素filter-name和url-pattern.用來定義Filter所對應的URL.
<filter-mapping>
<filter-name>Filter的名稱</filter-name>定義Filter的名稱.
<url-pattern>URL</url-pattern>Filter所對應的RUL.例如:<url-pattern>/Filter/Hello</url-pattern>
<servlet-name>Servlet的名稱<servlet-name>定義servlet的名稱.
<dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher>設定Filter對應的請求方式,有RQUEST,INCLUDE,FORWAR,ERROR四種,默認爲REQUEST.
</filter-mapping>
範例:
<filter-mapping>
<filter-name>GZIPEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.14)<mima-mapping>
mime-mapping包含兩個子元素extension和mime-type.定義某一個擴展名和某一MIME Type作對映.
<mima-mapping>
<extension>擴展名名稱</extension>擴展名稱
<mime-type>MIME格式</mime-type>MIME格式.
</mime-mapping>
範例:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ppt</extesnion>
<mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>
3.15)jsp相關配置
jsp-config元素主要用來設定JSP的相關配置,<jsp:config>包括<taglib>和<jsp-property-group>兩個子元素.其中<taglib>元素在JSP 1.2時就已經存在了;而<jsp-property-group>是JSP 2.0新增的元素。
<jsp-config>sql
taglib元素包含兩個子元素taglib-uri和taglib-location.用來設定JSP網頁用到的Tag Library路徑.
<taglib>
<taglib-uri>URI</taglib-uri>taglib-uri定義TLD文件的URI,JSP網頁的taglib指令能夠經由這個URI存取到TLD文件.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>TLD文件對應Web站臺的存放位置.
</taglib>
<jsp-property-group>jsp-property-group元素包含8個元素,分別爲:
<description>Description</descrition>此設定的說明
<display-name>Name</display-name>此設定的名稱
<url-pattern>URL</url-pattern>設定值所影響的範圍,如:/CH2 或者/*.jsp
<el-ignored>true|false</el-ignored>若爲true,表示不支持EL語法.
<scripting-invalid>true|false</scripting-invalid>若爲true表示不支持<%scription%>語法
<page-encoding>encoding</page-encoding>設定JSP網頁的編碼
<include-prelude>.jspf</include-prelude>設置JSP網頁的擡頭,擴展名爲.jspf
<include-coda>.jspf</include-coda>設置JSP網頁的結尾,擴展名爲.jspf
</jsp-property-group>
</jsp-config>
範例:數據庫
<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> <uri-pattern>/*</uri-pattern> <el-ignored>true</el-ignored> <page-encoding>GB2312</page-encoding> <scripting-inivalid>true</scripting-inivalid> ............ </jsp-property-group> </jsp-config>
3.16)資源管理對象配置:
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
3.17)<resource-ref>:資源工廠配置
resource-ref元素包括五個子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得站臺可利用資源.
<resource-ref>
<description>說明</description>資源說明
<rec-ref-name>資源名稱</rec-ref-name>資源名稱
<res-type>資源種類</res-type>資源種類
<res-auth>Application|Container</res-auth>資源由Application或Container來許可
<res-sharing-scope>Shareable|Unshareable</res-sharing-scope>資源是否能夠共享.默認值爲 Shareable
</resource-ref>
範例:
<resource-ref>
<description>JNDI JDBC DataSource of JSPBook</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSoruce</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.17)配置數據庫鏈接池就可在此配置
<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>
3.18)安全限制配置
<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>tomcat