1. 程式人生 > >javaWeb中的web.xml檔案的配置

javaWeb中的web.xml檔案的配置

在java工程中,web.xml用來初始化工程配置資訊,比如說welcome頁面,filter,listener,servlet,servlet-mapping,啟動載入級別等等。

每一個xml檔案都有定義他書寫規範的schema檔案,web.xml所對應的xml Schema檔案中定義了多少種標籤元素,web.xml中就可以出現它所定義的標籤元素,也就具備哪些特定的功能。web.xml的模式檔案是由Sun 公司定義的,每個web.xml檔案的根元素為<web-app>中,必須標明這個web.xml使用的是哪個模式檔案。

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中常用的標籤及其功能

1.<description>,<display-name>,<icon>

<description>專案描述</discription> 對專案做出描述.

<display-name>專案名稱</display-name> 定義專案的名稱.

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

2.<context-param>

<context-param>元素含有一對引數名和引數值,用作應用的servlet上下文初始化引數。引數名在整個Web應用中必須是惟一的。 context-param 元素用來設定web應用的環境引數(context),它包含兩個子元素: param-name和param-value.  <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.<filter>

filter元素用於指定Web容器中的過濾器。

在請求和響應物件被servlet處理之前或之後,可以使用過濾器對這兩個物件進行操作。

利用下一節介紹 的filter-mapping元素,過濾器被對映到一個servlet或一個URL模式。

這個過濾器的filter元素和filter-mapping 元素必須具有相同的名稱。

filter元素用來宣告filter的相關設定.filter元素除了下面介紹的的子元素之外,還包括之前介紹過的<icon>,<display-name>,<description>,<init-param>,其用途一樣.

下面介紹filter-name,filter-class和init-param元素 init-param元素與context-param 元素具有相同的元素描述符。

filter-name元素用來定義過濾器的名稱,該名稱在整個應用中都必須是惟一的。

filter-class元素指定過濾 器類的完全限定的名稱。

<filter-name>Filter的名稱</filter-name> 定義Filter的名稱. <filter-class>Filter的類名稱</filter-class> 定義Filter的類名稱.例如:com.foo.hello

例如:

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

4.<filter-mapping>

filter-mapping元素用來宣告Web應用中的過濾器對映。過濾器可被對映到一個servlet或一個URL模式。將過濾器對映到一個 servlet中會造成過濾器作用於servlet上。將過濾器對映到一個URL模式中則可以將過濾器應用於任何資源,只要該資源的URL與URL模式匹配。過濾是按照部署描述符的filter-mapping元素出現的順序執行的。

filter-mapping 元素的兩個主要子元素filter-name和url-pattern.用來定義Filter所對應的URL。還有servlet-name和dispatcher子元素,不是很常用。

<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-name>GZIPEncoding</filter-name>

<url-pattern>/*</url-pattern>

< /filter-mapping>

完整的filter配置例子如下:

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

5.<servlet>

在web.xml中完成的一個最常見的任務是對servlet或JSP頁面給出名稱和定製的URL。用servlet元素分配名稱,使用servlet-mapping元素將定製的URL與剛分配的名稱相關聯。

例如:

<servlet>

<servlet-name>Test</servlet-name>

<servlet-class>com.moreservlets.TestServlet</servlet-class>

< /servlet>

這表示位於WEB-INF/classes/com/moreservlets/TestServlet的servlet已經得到了註冊名Test。

6.<servlet-mapping>

servlet-mapping元素包含兩個子元素servlet-name和url-pattern.用來定義servlet所對應URL.

<servlet-name>Servlet的名稱</servlet-name> 定義Servlet的名稱. <url-pattern>Servlet URL</url-pattern> 定義Servlet所對應的RUL.例如:<url-pattern>/Servlet/Hello</url-pattern>

例如:

<servlet-mapping> <servlet-name>LoginChecker</servlet-name> <url-pattern>/LoginChecker</url-pattern>< /servlet-mapping>

完整的servlet配置如下所示:

<servlet> <servlet-name>ServletName</servlet-name> <servlet-class>xxxpackage.xxxServlet</servlet-class> <!--Servlet的類--> <init-param> <!--初始化一個變數,可看成全域性變數,可省略--> <param-name>引數名稱</param-name> <!--變數名稱--> <param-value>引數值</param-value> <!--變數值--> </init-param>< /servlet>< servlet-mapping> <servlet-name>ServletName</servlet-name> <url-pattern>/aaa/xxx</url-pattern> <!--對映的url路徑 -->< /servlet-mapping>

在位址列中輸入http://localhost:8080/web-App/aaa/xxx就可以訪問了。

7.<listener>

listener元素用來註冊一個監聽器類,可以在Web應用中包含該類。使用listener元素,可以收到事件什麼時候發生以及用什麼作為響應的通知。

listener元素用來定義Listener介面,它的主要子元素為<listener-class> <listen-class>Listener的類名稱</listener-class> 定義Listener的類名稱.例如: com.foo.hello 例如:

<listener> <listener-class>com.foo.hello</listener-class>< /listener>

8.<session-cofing>

session-config包含一個子元素session-timeout.定義web應用中的session引數. <session-timeout>分鐘</session-timeout> 定義這個web應用所有session的有效期限.單位為分鐘.

例如:

<session-config> <session-timeout>20</session-timeout>< /session-config>

9.<mime-mapping>

mime-mapping包含兩個子元素extension和mime-type.定義某一個副檔名和某一MIME Type做對映.

<extension>副檔名名稱</extension> 副檔名稱 <mime-type>MIME格式</mime-type> MIME格式.

例如:

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

10.<welcome-file-list>

welcome-file-list包含一個子元素welcome-file.用來定義首頁列單. <welcome-file>用來指定首頁檔名稱</welcome-flie> welcome-file用來指定首頁檔名稱.我們可以用<welcome-file>指定幾個首頁,而伺服器會依照設定的順序來找首頁.

例如:

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

11.<error-page>

error-page元素包含三個子元素error-code,exception-type和location.

將錯誤程式碼(Error Code)或異常(Exception)的種類對應到web應用資源路徑.

<error-code>錯誤程式碼</error-code> HTTP Error code,例如: 404 <exception-type>Exception</exception-type> 一個完整名稱的Java異常型別 <location>/路徑</location> 在web應用內的相關資源路徑

例如:

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

12.<jsp-config>

jsp-config元素主要用來設定JSP的相關配置,<jsp:config>包括<taglib>和<jsp-property-group>兩個子元素.其中<taglib>元素在JSP 1.2時就已經存在了;而<jsp-property-group>是JSP 2.0新增的元素.

<taglib> taglib元素包含兩個子元素taglib-uri和taglib-location.用來設定JSP網頁用到的Tag Library路徑. <taglib-uri>URI</taglib-uri> taglib-uri定義TLD檔案的URI,JSP網頁的taglib指令可以經由這個URI存取到TLD檔案. <taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction> TLD檔案對應Web應用的存放位置.  

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

12.<resource-ref>

resource-ref元素包括五個子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得應用可利用資源.

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