2017-06-16 61 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