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啟動時加載