Servlet web.xml welcome-file-list

web.xml文件中的web-app块的welcome-file-list子元素用于定义欢迎文件列表。 它的子元素是welcome-file,用于定义欢迎文件(即默认打开的页面)。

欢迎文件是服务器自动调用的文件,如果不指定任何文件名。

默认情况下,服务器按以下顺序查找欢迎文件:

  1. web.xml文件中的welcome-file-list指定的文件
  2. index.html
  3. index.html
  4. index.jsp

如果没有找到这些文件,服务器会报告404错误。

如果在web.xml中指定了welcome-file,并且所有文件index.htmlindex.htmlindex.jsp都存在,那么优先级将转到welcome-file

如果web.xml文件中不存在welcome-file-list项,那么优先级到index.html文件,然后是index.html,以及最后是index.jsp文件。

下面来看看一个定义欢迎文件的web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>helloworld</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>
</web-app>

现在,index.htmlindex.jsphome.jsp将是欢迎文件。

如果有欢迎文件,可以按如下所示的方式调用项目:

http://localhost:8888/helloproject

如上所示,我们并没有在项目名称(helloproject)之后指定任何文件名。上面URL访问相当于以下三个 -

http://localhost:8888/helloproject/index.html
http://localhost:8888/helloproject/index.jsp
http://localhost:8888/helloproject/home.jsp

上一篇: War文件 下一篇: Servlet启动时加载