一、概述
在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/n/179911.html