如何使用web.xml中的Servlet URL映射? [英] How are Servlet url mappings in web.xml used?

查看:127
本文介绍了如何使用web.xml中的Servlet URL映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含内容的web.xml文件:

I have a web.xml file with content:

<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>org.mycompany.test1</servlet-class>
</servlet>
<servlet>
    <servlet-name>servlet2</servlet-name>
    <servlet-class>org.mycompany.test2</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/path/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>servlet2</servlet-name>
    <url-pattern>/path/test/*</url-pattern>
</servlet-mapping>

我试过请求

.../path/test/abc 
.../path/test

两个请求都由Servlet2处理。为什么?

Both requests are processed by Servlet2. Why?

更新

谢谢你们的帮助。
我意识到行为取决于servlet映射声明的顺序。
我试过这个web.xml

Thank you guys for your help. I realised that behaviour depends on order of servlet-mapping declaration. I tried this web.xml

<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>org.mycompany.test1</servlet-class>
</servlet>
<servlet>
    <servlet-name>servlet2</servlet-name>
    <servlet-class>org.mycompany.test2</servlet-class>
</servlet>
<servlet>
    <servlet-name>servlet3</servlet-name>
    <servlet-class>org.mycompany.test3</servlet-class>
</servlet>
<servlet>
    <servlet-name>servlet4</servlet-name>
    <servlet-class>org.mycompany.test4</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/path/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>servlet2</servlet-name>
    <url-pattern>/path/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>servlet3</servlet-name>
    <url-pattern>/path/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>servlet4</servlet-name>
    <url-pattern>/path</url-pattern>
</servlet-mapping>

结果:

.../path/test/abc - servlet2
.../path/test/ - servlet2
.../path/test - servlet2

.../path/abc - servlet3
.../path/ - servlet4
.../path - servlet4


推荐答案

来自Servlet 3.0 规范,这是Web容器在收到请求后必须找到servlet的方式(强调我的):

From Servlet 3.0 specification, this is how the web container must locate the servlet after receiving a request (emphasis mine):


路径用于映射到servlet的是来自
请求对象的请求URL减去上下文路径和路径参数。下面的
URL路径映射规则按顺序使用。 第一个成功的
匹配用于未尝试进一步匹配


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

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

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

  4. 如果前三个规则都没有导致servlet匹配,则容器将尝试提供适合所请求的
    资源的内容。如果为
    应用程序定义了默认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. Many containers provide an implicit default servlet for serving content.

容器必须使用区分大小写的字符串比较进行匹配。

The container must use case-sensitive string comparisons for matching.

您还应该查看映射的规范(如下所示):

You should also look the specification of mappings (given below):


在Web应用程序部署描述符中,以下语法是
用于定义映射:

In the Web application deployment descriptor, the following syntax is used to define mappings:


  • '/'字符开头并以'/ *'后缀$ b $结尾的字符串b用于路径映射。

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

使用以'*。'开头的字符串。前缀作为扩展映射。

A string beginning with a ‘*.’ prefix is used as an extension mapping.

空字符串()是一种特殊的URL模式映射到
应用程序的上下文根,即
http:// host:port /< contextroot> / 形式的请求。在这种情况下,路径信息是'/'
servlet路径和上下文路径是空字符串()

The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string ("").

仅包含'/'字符的字符串表示默认
应用程序的servlet。在这种情况下,servlet路径是
请求URI减去上下文路径,路径信息为null。

A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

所有其他字符串用于确切仅匹配

All other strings are used for exact matches only

现在让我们看一下示例。考虑以下映射集:

Let us look at examples now. Consider the following set of mappings:


Path Pattern            Servlet
/foo/bar/*              servlet1
/baz/*                  servlet2
/catalog                servlet3
*.bop                   servlet4

将导致以下行为:


Incoming Path           Servlet Handling Request
/foo/bar/index.html     servlet1
/foo/bar/index.bop      servlet1
/baz                    servlet2
/baz/index.html         servlet2
/catalog                servlet3
/catalog/index.html     "default" servlet
/catalog/racecar.bop    servlet4
/index.bop              servlet4

请注意,对于 /catalog/index.html /catalog/racecar.bop
servlet映射到/ catalog未使用,因为匹配不准确。

Note that in the case of /catalog/index.html and /catalog/racecar.bop, the servlet mapped to "/catalog" is not used because the match is not exact.

现在出现问题:)

/ path / test 属于映射规范的第5点。这意味着只有以 / path / test 结尾的路径才会定位 servlet1

/path/test belongs to the 5th point of specification of mappings. Which means only the paths ending in /path/test will target servlet1.

然而 / path / test / * 符合相同规格的第一点。这意味着:

However /path/test/* qualifies for the first point of the same specification. This means that:

... / path / test 将由 servlet1处理

... / path / test / abc 将由<处理code> servlet2

我在测试应用程序中验证了这一点。

Which was verified by me in a test application.

这篇关于如何使用web.xml中的Servlet URL映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆