1. 程式人生 > >九、web.xml理解

九、web.xml理解

接口 cte rar ptc .sql 初始化 數據庫 instance tag

1、web.xml文件在每個web工程不是必須要有的:
web.xml文件是用來初始化配置信息:比如Welcome頁面、servlet、servlet-mapping、filter、listener、啟動加載級別等。當web工程沒用到這些時,你可以不用web.xml文件來配置你的Application。

2、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 的順序進行調用的。

3、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接口的類相關聯 比如設置一個編碼過濾器,過濾所有資源

技術分享圖片
<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>
View Code

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>

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

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>

九、web.xml理解