1. 程式人生 > >面試題:J2EE中web.xml配置文件詳解

面試題:J2EE中web.xml配置文件詳解

ppi targe nor tag 2.4 -type exti tab 標簽

一、web.xml是什麽

web.xml學名叫部署描述符文件,是在Servlet規範中定義的,是Web應用的配置文件,是Web應用的基礎。

二、web.xml加載流程

總的來說:ServletContext——Listener——Filter——Servlet

1、首先Web容器創建一個ServletContext對象(對應JSP中的application內置對象),這個Web項目所有部分都將共享這個上下文(類似於這個項目的全局變量集合)。

2、然後Web容器以<contex-param></contex-param>中的name為鍵,value作為值,將其轉化為鍵值對,存入ServletContext對象中。

3、Web容器創建<listener></listener>中的類實例,根據配置的class類路徑<listener-class>來創建監聽器,在監聽類中會有contextInitialized(ServletContextEvent args)初始化方法,啟動Web應用時,系統調用Listener的該方法,在這個方法中獲得:

ServletContext application =ServletContextEvent.getServletContext();
context-param的值= application.getInitParameter("context-param的鍵");

得到這個context-param的值之後,你就可以做一些操作了。
舉例:你可能想在項目啟動之前就打開數據庫,那麽這裏就可以在<context-param>中設置數據庫的連接方式(驅動、url、user、password),在監聽器中初始化數據庫的連接。這個監聽器是自己寫的一個類,除了初始化方法,它還有銷毀方法,用於關閉應用前釋放資源。比如:說數據庫連接的關閉,此時,調用contextDestroyed(ServletContextEvent args)銷毀方法,關閉Web應用時,系統調用Listener的該方法。

4、接著,容器會讀取<filter></filter>,根據指定的類路徑來實例化過濾器。

5、以上都是在Web項目還沒有啟動完畢之前完成的工作,而Servlet是在第一次客戶端向服務器發起請求時被實例化的

(過濾器與監聽器相關問題可參看另一篇博客點擊打開鏈接)

三、web.xml詳述

每一個站的WEB-INF下都有一個web.xml的設定文件,它提供了我們站臺的配置設定.
web.xml定義:
.站臺的名稱和說明
.針對環境參數(Context)做初始化工作
.Servlet的名稱和映射
.Session的設定
.Tag library的對映
.JSP網頁設定
.Mime Type處理
.錯誤處理
.利用JDNI取得站臺資源

要了解web.xml的設定值,必須了解它的schema,從web.xml中知道它的schema是由Sum Microsystems公司定制的,如果你想更為詳細的了解它,
可以到http://java.sun.com/xml/ns/j2ee/web-mapp_2_4.xsd網頁,那裏有更為詳細的介紹。這裏我介紹我們平常見得最多的.

   1: <?xml version="1.0" encoding="ISO-8859-1"?> 
   2:  
   3: <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
   4:     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   5:     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
   6:     version="2.4">
   7: <web-app>

這是一般在寫XML時所做的聲明,定義了XML的版本,編碼格式,還有重要的指明schema的來源,為http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd.

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.
範例:

   1: <display-name>Develop Example</display-name>
   2: <description>JSP 2.0 Tech Book‘s Examples</description>
   3: <icon>
   4:    <small-icon>/images/small.gif</small-icon>
   5:    <large-icon>/images/large.gir</large-icon>
   6: </icon> 
   7:  
   8: <distributable>

2、<distributable>
______________________________________
distributable 元素為空標簽,它的存在與否可以指定站臺是否可分布式處理.如果web.xml中出現這個元素,則代表站臺在開發時已經被設計為能在多個JSP Container 之間分散執行.
範例:

   1: <distributable/> 

3、<context-param>
___________________________________

<context-param>
context-param 元素用來設定web站點的環境參數(context),它包含兩個子元素:param-name和param-value.

<param-name>參數名稱</param-name>
設定Context名稱

<param-value>值</param-value>
設定Context名稱的值

</context-param>範例:

   1: <context-param>
   2:    <param-name>param_name</param-name>
   3:    <param-value>param_value</param-value>
   4: </context-param>


此所設定的參數,在JSP網頁中可以使用下列方法來取得:

   1: ${initParam.param_name}


若在Servlet可以使用下列方法來獲得:

   1: String param_name=getServletContext().getInitParamter("param_name"); 

又如Spring framework在這裏可以設置context參數

   1: <!-- spring的配置 -->
   2: <context-param>
   3:     <param-name>contextConfigLocation</param-name>
   4:     <param-value>classpath:/SpringContext/applicationContext-web.xml
   5:     </param-value>
   6: </context-param>

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

<filter-name>Filter的名稱</filter-name>

定義Filter的名稱.

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

範例:

   1: <filter>
   2:   <filter-name>setCharacterEncoding</filter-name>
   3:   <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
   4:   <init-param>
   5:      <param-name>encoding</param-name>
   6:      <param-value>GB2312</param-value>
   7:   </init-param>
   8: </filter> 
   9:  
  10: <filter-mapping>

又如Struts2可以在這裏設filter

   1: <filter>
   2:     <filter-name>struts2</filter-name>
   3:     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
   4:     </filter-class>
   5:     <init-param>
   6:         <param-name>config</param-name>
   7:         <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
   8:     </init-param>
   9: </filter>
  10:  
  11: <filter-mapping>
  12:     <filter-name>struts2</filter-name>
  13:     <url-pattern>*.do</url-pattern>
  14: </filter-mapping>

5、<filter-mapping>
______________________________________
filter-mapping 元素的兩個主要子元素filter-name和url-pattern.用來定義Filter所對應的URL.

<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>範例:

   1: <filter-mapping>
   2:    <filter-name>GZIPEncoding</filter-name>
   3:    <url-pattern>/*</url-pattern>
   4: </filter-mapping> 

6、<servlet>
___________________________________________
<servlet-name>servlet的名稱</servlet-name>
<display-name>顯示名稱</display-name>
<servlet-class>servlet對應的class名

</servlet-class>範例:

   1: <servlet>
   2:     <servlet-name>org.apache.jsp.index_jsp</servlet-name>
   3:     <servlet-class>org.apache.jsp.index_jsp</servlet-class>
   4: </servlet>

7、<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>範例:

   1: <servlet-mapping>
   2:    <servlet-name>LoginChecker</servlet-name>
   3:    <url-pattern>/LoginChecker</url-pattern>
   4: </servlet-mapping> 

8、<listener>
___________________________________________
<listener>
listener元素用來定義Listener接口,它的主要子元素為<listener-class>

<listen-class>Listener的類名稱</listener-class>
定義Listener的類名稱.例如: com.foo.hello

<listener>
範例:

   1: <listener>
   2:   <listener-class>coreservlet.javaworld.CH11.ContenxtListener</listener-class>
   3: </listener> 

又如Spring framework可以在這裏設listener

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

9、 <session-cofing>
__________________________________
<session-config>
session-config包含一個子元素session-timeout.定義web站臺中的session參數.

<session-timeout>分鐘</session-timeout>
定義這個web站臺所有session的有效期限.單位為分鐘.

</session-config>範例:

   1: <session-config>
   2:    <session-timeout>20</session-timeout>
   3: </session-config> 


10、<mime-mapping>

___________________________________________________

<mima-mapping>
mime-mapping包含兩個子元素extension和mime-type.定義某一個擴展名和某一MIME Type做對映.

<extension>擴展名名稱</extension>
擴展名稱

<mime-type>MIME格式</mime-type>
MIME格式.

</mime-mapping>範例:

   1: <mime-mapping>
   2:    <extension>doc</extension>
   3:    <mime-type>application/vnd.ms-word</mime-type>
   4: </mime-mapping>
   5: <mime-mapping>
   6:    <extension>xls</extension>
   7:    <mime-type>application/vnd.ms-excel</mime-type>
   8: </mime-mapping>
   9: <mime-mapping>
  10:    <extension>ppt</extesnion>
  11:    <mime-type>application/vnd.ms-powerpoint</mime-type>
  12: </mime-mapping> 

11、<welcome-file-list>
_____________________________________________
<welcome-file-list>
welcome-file-list包含一個子元素welcome-file.用來定義首頁列單.

<welcome-file>用來指定首頁文件名稱</welcome-flie>
welcome-file用來指定首頁文件名稱.我們可以用<welcome-file>指定幾個首頁,而服務器會依照設定的順序來找首頁.

範例:

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

12、<error-page>

_________________________
<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>
範例:

   1: <error-page>
   2:    <error-code>404</error-code>
   3:    <location>/error404.jsp</location>
   4: </error-page>
   5: <error-page>
   6:    <exception-type>java.lang.Exception</exception-type>
   7:    <location>/except.jsp</location>
   8: </error-page>  

13、<jsp-config>
_______________________________________________
<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站臺的存放位置.

</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>範例:

   1: <jsp-config>
   2: <taglib>
   3:    <taglib-uri>Taglib</taglib-uri>
   4:    <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
   5: </taglib>
   6: <jsp-property-group>
   7:    <description>
   8:       Special property group for JSP Configuration JSP example.
   9:    </description>
  10:    <display-name>JSPConfiguration</display-name>
  11:    <uri-pattern>/*</uri-pattern>
  12:    <el-ignored>true</el-ignored>
  13:    <page-encoding>GB2312</page-encoding>
  14:    <scripting-inivalid>true</scripting-inivalid>
  15:    ............
  16: </jsp-property-group>
  17: </jsp-config> 

14、<resource-ref>

________________________________________________
<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
範例:

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

這些都是些比較常用的,詳細可以登錄: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

面試題:J2EE中web.xml配置文件詳解