繁体   English   中英

web.xml URL模式

[英]web.xml URL pattern

谁能给我有关设置URL模式的规则的信息,如果我将/用作索引页,还需要使用request.getRequestDispatcher("/html/file.html").forward(request,response)

文件file.htmlwar文件夹下的html文件夹中, html文件夹在WEB-INF的同一文件夹中

有人可以给我建议吗? 谢谢

您可以如下在web.xml中定义一个servlet,然后使用request.getRequestDispatcher("file").forward(request,response) ,实际上将发生的是将请求分发到映射为/file的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 规范@BalusC answer ):

1.路径映射:

  • 如果要创建路径映射,则以/开头映射,并以/*结尾。 例如:

     <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规范的摘要图:

在此处输入图片说明

您需要获取Java Servlet Specification 3.1的副本并阅读。

特别是第10和12章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM