xml中有两个与请求匹配的servlet映射会发生什么

xml中有两个与请求匹配的servlet映射会发生什么

本文介绍了如果我在web.xml中有两个与请求匹配的servlet映射会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在web.xml中有两个与请求匹配的servlet映射,会发生什么?它选择最具体的吗?

What happens if I have two servlet mappings in web.xml that match a request? Does it choose most specific?

例如,如果我有以下xml,并且在转到somethingservlet或everything_else servlet时请求来.... /某事

For example if I have the following xml and a request comes to ..../something while it go to somethingservlet or everything_else servlet?

   <servlet-mapping>
      <servlet-name>something</servlet-name>
      <url-pattern>/something</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>everything_else</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>


推荐答案

将使用第一次成功的比赛。

First succesful match will be used.

servlet容器后面有一些映射规则。阅读Servlet 2.5规范章节SRV.11:

There are certain mapping rules follwed by servlet container. Read Servlet 2.5 specification chapter SRV.11:


  1. 容器将尝试查找路径的完全匹配请求到servlet的路径。成功匹配选择
    servlet。

  2. 容器将递归尝试匹配最长的路径前缀。这是通过一次踩下路径树一个目录来完成
    ,使用'/'字符作为
    路径分隔符。最长匹配确定所选的servlet。

  3. 如果URL路径中的最后一个段包含扩展名(例如.jsp),则servlet
    容器将尝试匹配一个servlet处理扩展请求。
    扩展名被定义为最后一个'。'字符后的最后一个段的一部分。

  4. 如果前三个规则都没有导致servlet匹配,则容器将
    尝试提供适合所请求资源的内容。如果为应用程序定义了默认
    servlet,则会使用它。

  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.


这篇关于如果我在web.xml中有两个与请求匹配的servlet映射会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:54