servlet过滤器如何识别下一个目标是另一个过滤器还是servlet / jsp? [英] How does a servlets filter identify next destination is another filter or a servlet/jsp?

查看:89
本文介绍了servlet过滤器如何识别下一个目标是另一个过滤器还是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 < url-pattern> / *< / url-pattern> 已经存在我们使用的过滤器。究竟这里的实际问题就开始了。

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中,那么heck容器如何知道如何跟随< url-pattern> / ABCXYZ< / url-pattern> 而不是< url-pattern> / *< / url-pattern> ?请在这方面分享您的观点和知识。

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映射。 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.

最后,模式< url-pattern> /< / url-pattern> 总是匹配任何其他模式不匹配的请求。

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.

网址格式匹配

如果您不想转到该链接,则会从以下链接中复制。

请查看网址格式在哪里描述细节与例子。

Please have a look at URL Patterns where it is described in detail with examples.

这篇关于servlet过滤器如何识别下一个目标是另一个过滤器还是servlet / jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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