2017-06-16 90 views
0

任何人都可以給我關於設置網址格式的規則信息,如果我使用/作爲我的索引頁,也需要使用request.getRequestDispatcher("/html/file.html").forward(request,response)web.xml網址格式

文件file.html是在html文件夾是war文件夾下,文件夾html處於WEB-INF

同一文件夾中的任何一個都可以給我建議? 謝謝

+0

沒有真正理解你的問題... –

回答

1

您可以在web.xml中,如下定義一個servlet,然後用request.getRequestDispatcher("file").forward(request,response),基本上會發生什麼事是你將派遣你的要求到Servlet其映射爲/file和這個servlet將指向你的資源/html/file.html 。請注意,即使元素名稱是jsp-file,但您可以從中指出HTML。

<servlet> 
    <servlet-name>FileServlet</servlet-name> 
    <jsp-file>/html/file.html</jsp-file> 
</servlet> 
<servlet-mapping> 
    <servlet-name>FileServlet</servlet-name> 
    <url-pattern>/file</url-pattern> 
</servlet-mapping> 

作爲附加 - 來如何URL模式存在於web.xml文件中的serlvet映射相匹配,下面是在web.xml servlet映射規則(的此來源是 - Servlet specs@BalusC answer和):

1.路徑映射:

  • 如果你想創建一個路徑映射然後啓動映射無線th /並結束它將/*。例如:

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern>/foo/bar/*</url-pattern> <!-- http://localhost:7001/MyTestingApp/foo/bar/index.html would map this servlet --> 
    </servlet-mapping> 
    

2.擴展映射:

  • 如果你想創建一個擴展名映射,然後有servlet映射*.。例如:

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern>*.html</url-pattern> <!-- http://localhost:7001/MyTestingApp/index.html would map this servlet. Also, please note that this servlet mapping would also be selected even if the request is `http://localhost:7001/MyTestingApp/foo/index.html` unless you have another servlet mapping as `/foo/*`. --> 
    </servlet-mapping> 
    

3.默認servlet映射:

  • 假設你要定義,如果一個映射不匹配任何的servelt映射的話,應該是映射到默認的servlet,然後將servlet映射爲/。例如:

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern>/</url-pattern> <!-- Suppose you have mapping defined as in above 2 example as well, and request comes for `http://localhost:7001/MyTestingApp/catalog/index.jsp` then it would mapped with servlet --> 
    </servlet-mapping> 
    

4。精確匹配映射:

  • 假設你要定義精確匹配映射那麼就不要使用任何通配符或什麼的,並定義精確匹配,如/catalog。例如:

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern>/catalog</url-pattern> <!-- Only requests with http://localhost:7001/MyTestingApp/catalog will match this servlet --> 
    </servlet-mapping> 
    

5.應用上下文根映射:

  • 空字符串""是準確映射到 應用程序的上下文根特殊URL模式。即形式爲http://localhost:7001/MyTestingApp/的請求。

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern></url-pattern> <!-- Only requests with http://localhost:7001/MyTestingApp/ will match this servlet Please note that if request is http://localhost:7001/MyTestingApp/abc then it will not match this mapping --> 
    </servlet-mapping> 
    

6.匹配所有映射:

  • 如果你想匹配一個映射的請求或覆蓋所有其他servlet映射,然後創建一個映射爲/*

    <servlet-mapping> 
        <servlet-name>FileServlet</servlet-name> 
        <url-pattern>/*</url-pattern> <!-- This will override all mappings including the default servlet mapping --> 
    </servlet-mapping> 
    

下面是JMS規範的概要圖:

enter image description here