servlet 过滤器如何识别下一个目标是另一个过滤器还是 servlet/jsp? [英] How does a servlets filter identify next destination is another filter or a servlet/jsp?
问题描述
我们通常会在 web.xml 中为 servlet 中的任何过滤器编写 <url-pattern>/*</url-pattern>
.
We usually end up with writing <url-pattern>/*</url-pattern>
in web.xml for any Filter in servlets.
<filter-mapping>
<filter-name>requestRedirectorFilter</filter-name>
<url-pattern>/action</url-pattern>
</filter-mapping>`.
现在我的疑问是 java 如何识别下一个 servlet/jsp 是哪个?因为我们通过
Now my doubt is how java identifies which is next servlet/jsp is? Because any request we make through
request.getRequestDispatcher("/ABCXYZ").forward(request, (HttpServletResponse)servletResponse);
要在下一个 servlet/jsp 上导航,默认情况下容器将在 web.xml 中搜索.在 web.xml 中,
已经存在于我们使用的过滤器中.正是在这里,实际问题开始了.
to navigate on next servlet/jsp, container by default is going to search in web.xml. And in web.xml <url-pattern>/*</url-pattern>
is already there for the filter we use. Exactly here actual problem begins.
如果 <url-pattern>/*</url-pattern>
[它充当任何请求的通用接收器] 已经存在于 web.xml 中,那么容器如何知道遵循
而不是
?请分享您对这方面的看法和知识.
If <url-pattern>/*</url-pattern>
[which is acting like a universal receiver for any request] is already there in web.xml then How the heck container knows to follow <url-pattern>/ABCXYZ</url-pattern>
instead <url-pattern>/*</url-pattern>
? Please share your views and knowledge on this front.
推荐答案
Servlet 匹配过程
一个请求可能在给定的上下文中匹配多个 servlet-mapping.servlet 容器使用简单的匹配过程来确定最佳匹配.
A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match.
匹配过程有四个简单的规则.
首先,与通配符路径匹配相比,容器更喜欢精确路径匹配.
其次,容器更喜欢匹配最长的模式.
Second, the container prefers to match the longest pattern.
第三,容器更喜欢路径匹配而不是文件类型匹配.
Third, the container prefers path matches over filetype matches.
最后,模式
总是匹配任何没有其他模式匹配的请求.
Finally, the pattern <url-pattern>/</url-pattern>
always matches any request that no other pattern matches.
例如,上下文 web.xml 文件可以将在线目录的主页映射到一种模式,将目录的搜索页面映射到不同的模式,如下所示:
For example, a context web.xml file can map the home page for an online catalog to one pattern and the search page for the catalog to a different pattern, as shown below:
<servlet-mapping>
<servlet-name>catalogBrowse</servlet-name>
<url-pattern>/Catalog/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>catalogSearch</servlet-name>
<url-pattern>/Catalog/search/*</url-pattern>
</servlet-mapping>
下图说明了上下文的匹配过程.由于容器更喜欢匹配最长模式,因此包含 /Catalog/search/
的 URL 始终匹配 catalogSearch 的映射,而不是 catalogBrowse 的映射.
Below figure illustrates the matching process for a context. Since the container prefers to match the longest pattern, a URL that includes /Catalog/search/
always matches the mapping for catalogSearch rather than the mapping for catalogBrowse.
网址格式匹配
如果您不感兴趣,请从以下链接复制.
请查看 URL 模式,其中详细描述了示例.
Please have a look at URL Patterns where it is described in detail with examples.
这篇关于servlet 过滤器如何识别下一个目标是另一个过滤器还是 servlet/jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!