即使 <welcome-file-list> 未定义,index.jsp 文件也会打开

index.jsp file opens even when the <welcome-file-list> is not defined

我在 Eclipse Luna 中编写了一个简单的动态 Web 项目。在 web.xml 页面中,我删除了默认的 welcome-file-list 标签。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>indextest</display-name>
</web-app>

但是 url http://localhost:8080/indextest/ 仍然指向 'WEB-INF' 下的 index.jsp 即使我从 web.xml 中删除了标签 welcome-file-list .尽管 web.xml 中没有 welcome-file-list,它如何指向 index.jsp 页面?

如果没有提供欢迎列表,那么容器将尝试按照定义的顺序加载以下文件:

  1. index.html
  2. index.htm
  3. index.jsp

更新: 关于 tomcat

如果应用程序中没有提供web.xml,则向应用程序提供Tomcat的默认web.xml($CATALINA_HOME/conf/web.xml) .此部署描述符包含以下几行:

<!-- -->
<!-- If you define welcome files in your own application's web.xml -->
<!-- deployment descriptor, that list *replaces* the list configured -->
<!-- here, so be sure to include any of the default values that you wish -->
<!-- to use within your application. -->

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

这就是默认显示 index.jsp 的原因

更新来源:

如果您使用的是 Tomcat 7 实例,并且没有指定欢迎文件列表,则容器 (tomcat) 正在查看他的默认值,即 /conf/web.xml 在你的 tomcat 实例中。

这些是行:

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

我建议不要更改 tomcat 默认值中的任何内容,因为您的 Web 应用程序不应依赖于运行它的容器。相反,您应该在自己的 web.xml 中定义自己的欢迎文件列表。 希望这对您有所帮助!

默认文件会调用index.jsp如果你改变文件名然后它不会找到index.jsp并且你可以得到预期的结果。