关于一个web站点的欢迎页面

  • 对于一个webapp来说,我们可以设置一个欢迎页面,访问这个webapp时如果没有任何的资源路径,就会访问它

    • 这是一般的访问方式

    • 如果没有任何的资源路径

      • localhost:8080/finalServlet 访问的只是站点时,默认访问欢迎页面

  • 如何设置:(webapp名字为wel)

    单独设置一个名login的html文件,这就是欢迎页面

    在xml文件中配置如下信息:

         <welcome-file-list>
             <!--路径不要以斜杠开始-->
             <welcome-file>Login.html</welcome-file>
      </welcome-file-list>

    切记一定只访问到项目名字,效果如图:image-20230518164956046

    • 设置多个欢迎页面

         <welcome-file-list>
             <welcome-file>page1/page2/Login.html</welcome-file>
         </welcome-file-list>
    • 越靠上的优先级越高,找不到则继续往下找

     

    如果没有设置欢迎页面,正常来讲应该是404,但是即使我能访问的只是站点,也能显示出页面,因为如果页面名字为:index.html 时,Tomcat提前设置好,不用设置也可以自动跳转到 index.html 页面。

    仍然报错:

    image-20230518170213659

    删掉index.jsp:image-20230518192220643

终于显示出404:image-20230518192250916

  • 实际上,有两个地方可以配置欢迎页面

    • webapp内部的web.xml文件中(局部配置)

    • /conf/web.xml文件中(全局配置)

     <!--文件中这样定义-->
     <welcome-file-list>
         <welcome-file>index.html</welcome-file>
         <welcome-file>index.htm</welcome-file>
         <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>

    如果自己设置了欢迎页面,全局变量就失效了

    • 局部优先原则(就近原则)

  • 欢迎页面是一个资源,可动态可静态,可以是一个sevlet,将html的路径改为servlet类的路径即可,路径不能以斜杠开头

posted @ 2023-05-18 19:36  hangsingplus  Views(137)  Comments(0Edit  收藏  举报