一、概述
在JavaWeb開發中,welcome-file-list 的作用是指定Web應用程序的默認頁面。也就是說,當用戶請求訪問應用程序URL時,如果不指定具體文件名稱,就會自動定位到welcome-file-list 指定的默認文件或者默認目錄下的文件。
比如,當用戶訪問http://localhost:8080/時,就會自動打開/welcome-file-list 指定的默認頁面,如果沒有設置,則顯示目錄結構。
二、語法
<welcome-file-list> <welcome-file>文件名稱1</welcome-file> <welcome-file>文件名稱2</welcome-file> <welcome-file>文件名稱3</welcome-file> </welcome-file-list>
註:其中,welcome-file 屬性中描述的文件名必須放在Web應用程序的部署位置下。
三、多個默認頁面設置
在實際開發中,我們一般會針對不同的請求路徑,設置不同的默認頁面。這時,在默認頁面設置這一塊常常涉及到多個文件配置,可以使用多個 指定多個文件名稱。
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> </welcome-file-list>
四、文件目錄設置
有時候,我們希望訪問Web應用程序根目錄時,在展現固定的頁面的同時,將一些靜態頁面的目錄結構也展現出來。這時可以將目標頁面放在某個目錄下,使用目錄名稱作為welcome-file-list 子元素中文件名稱的一部分。
<welcome-file-list> <welcome-file>welcome.jsp</welcome-file> <welcome-file>static/html/index.html</welcome-file> </welcome-file-list>
五、過濾器配置
在實際開發中,我們常常會使用過濾器(Filter)去實現Web應用程序的攔截和預處理,對用戶的請求進行相應地處理和管理,這時,需要在 元素中添加過濾器的名稱和順序。
<welcome-file-list> <welcome-file>welcome.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> <filter-name>myFilter</filter-name> <filter-class>com.xxx.MyFilter</filter-class> </welcome-file> </welcome-file-list>
六、擴展
在 Welcome 文件列表中,需要注意以下幾點:
- 只有 Web 應用根目錄中存在的文件或目錄才能成為 Wellcome 文件列表的一部分;
- 當沒有配置 Welcome 文件時,Web 容器將默認使用一個名稱為 index 的文件,文件擴展名根據 Web Container 的部署平台而定(如:.html、.htm等);
- 多個同名的 Welcome 文件,第一個匹配的文件會被使用,其它的會被忽略;
- 在擴展名相同的情況下,Tomcat 的 Welcome page 配置會使用默認文件名列表,而 GlassFish 將不會,它將依據 Java Servlet 規範在 Servlet 容器根目錄下查找 <welcome-file-list> 指定的文件名稱.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/179911.html