java - 在 web.xml 中映射案例

标签 java xml web-applications web.xml url-mapping

web.xml 中,是否可以有一个主映射和一个与第一个映射不同的第二个映射,如下所示?

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ErrorHandler</servlet-name>
    <url-pattern>/?</url-pattern>
</servlet-mapping>

最佳答案

您可以有多个servlet-mapping部署描述符中的 s。 Java Servlet Specification 中描述了选择映射的规则。第 12 章将请求映射到 Servlet:

12.1 Use of URL Paths
...
The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters. The URL path mapping rules below are used in order. The first successful match is used with no further matches attempted:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

这意味着,如果您有像 <url-pattern>/tar</url-pattern> 这样的完全匹配项,将选择该映射。否则,将使用第二个不太具体的映射(例如 <url-pattern>/*</url-pattern> )。

但是您无法使用 <url-pattern>/tar*</url-pattern> 创建映射,因为星号只能在末尾 /*或开头*.模式的(注意星号之前的斜杠 / 和星号之后的句点 .)。有关完整详细信息,请参阅第 12.2 节“映射规范”。

关于java - 在 web.xml 中映射案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30562128/

相关文章:

java - 如何在 Java 和 Android 中从 JSON 读取想要的数据

xml - 如何在 xmlinputformat hadoop 中给带有属性的 xml 开始标记

django - 将 Node.js 与 Django 和 Backbone.js 一起使用

c# - LogManager.GetLogger ("Name") 和 LogManager.GetCurrentClassLogger() 有什么区别?

Java - HTTP 状态 404 - 未找到

java - 使用反射更改 boolean 字符串值不起作用

java - 全局事务可以/是​​否跨越多个线程?

java - 适用于 Mindflow 类型应用程序的良好 API

java - 如何在 Java 中读取在线 XML 文件以获取货币汇率

java - 使用 SAXParser 读取 S3 中的 XML 文件