servlet/servlet-mapping 和 filter/filter-mapping 之间的区别?
作为探索/学习 Struts2、JSP 和 Servlet 的一部分,我从此处查看 和 可以在 web.xml
中使用 servlet
和 servlets-mapping
。然而,Struts2 也为 web.xml
提到了 filters
和 filter-mapping
。
两者有什么区别?这些是互相排斥的吗?我什么时候应该使用哪个以及为什么?有人可以澄清这些概念吗?谢谢。
澄清
我刚刚明白我需要了解 Struts2 和 Servlet 之间的关系:http://www.coderanch.com/t/57899/Struts/Difference- Between-servlet-struts
As part of exploring/learning Struts2, JSP and Servlets, I see from here and there that servlets
and servlets-mapping
can be used in web.xml
. However, Struts2 mentions filters
and filter-mapping
too for web.xml
.
What is the difference between both? Are these mutually exclusive? When should I use which and why? Can someone clarify the concepts? Thanks.
CLARIFICATION
I just got to understand that I needed to understand how Struts2 and Servlets are related: http://www.coderanch.com/t/57899/Struts/Difference-between-servlet-struts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Servlet 过滤器实现拦截过滤器 模式。虽然 servlet 是 Web 请求的最终目标,但每个请求都会经过一系列过滤器。每个过滤器都可以在进一步传递请求之前修改请求,或者在从 servlet 接收返回请求之后进行响应。它甚至可以放弃进一步传递请求并像 servlet 一样完全处理它(并不罕见)。例如,缓存过滤器可以在不调用实际 servlet 的情况下返回结果。
Servlet filters implement intercepting filter pattern. While servlet is the ultimate target of web request, each request goes through a series of filters. Every filter can modify the request before passing it further or response after receiving it back from the servlet. It can even abstain from passing the request further and handle it completely just like servlet (not uncommon). For instance caching filter can return result without calling the actual servlet.
过滤器的使用方式与 Servlet 过滤器类似。例如,如果您需要对某些 URL 进行安全检查,那么您可以为这些页面添加过滤器。例如,您可以说
/secure/pages/*.do
需要被 securityFilter 拦截。然后,SecurityFilter 类(实现 Filter 接口的类)的 doFilter() 方法将在将安全审核转发到实际请求的 servlet 之前处理安全审核。Servlet 几乎是标准的东西。您定义一个 servlet,然后让 servlet 容器知道需要映射到该 servlet 的请求类型。
它们并不相互排斥。它们可以同时使用。把过滤器想象成这个词的意思——它在继续下一个 servlet/操作之前“过滤”事物(日志记录、安全性等)。
Filters are used like Servlet Filters. For example, if you need to do security checks on certain URLs then you can add a filter for those pages. For instance, you can say
/secure/pages/*.do
needs to be intercepted by securityFilter. Then thedoFilter()
method of the SecurityFilter class (a class that implements the Filter interface) will handle the security audit before forwarding it to the actual requesting servlet.Servlets are pretty much the standard stuff. You define a servlet and then let the servlet container know what type of requests needs to be mapped to that servlet.
They are not mutually exclusive. They both can be used at the same time. Think of filter like the way the word means - it "filters" things (logging, security,etc) before proceeding to the next servlet/action.
根据 servlet 规范,请求生命周期在最终由 servlet 执行之前要经过一系列过滤器。
当您查看 Filter 接口中 doFilter 方法的签名时,这是相当直观的。
也就是说,在过滤器中您可以访问请求、响应以及链。约定是,作为实现者,您应该在过滤器中执行的操作之前或之后调用
链
,或者如果不希望继续执行则根本不调用。调用chain.doFilter(...)
将导致过滤器链中的下一个过滤器(其映射与所请求的 URL 匹配)被执行。该链的最后一个成员是 Servlet,其映射与请求的 URL 相匹配。从技术上讲,您可以在过滤器中执行在 Servlet 中可以执行的所有操作。您可以构建应用程序来在过滤器中执行所有处理和渲染,并拥有一个不执行任何操作的空白 servlet。主要区别在于,如果没有针对给定 URL 映射的 servlet,则容器必须以 404 错误进行响应,因此必须始终有一个针对您想要提供服务的任何 URL 进行映射的 servlet。您也可以只将一个 servlet 映射到一个 URL,但可以有任意数量的过滤器。
The request lifecycle according to the servlet specification goes through a chain of filters before finally being executed by a servlet.
This is fairly intuitive when you look at the signature for the doFilter method in the
Filter
interfaceThat is, in the filter you have access to the request and response and the chain. The contract is that you, as implementer, should invoke the
chain
either before or after the operations you do in the filter, or not at all if it is desired to not continue execution. Callingchain.doFilter(...)
will cause the next filter in the chain of filters with a mapping matching the requested URL to be executed. The final member of the chain is the servlet whose mapping matches the requested URL.Technically, you can do everything in a filter that you can do in a servlet. You can build your application to do all processing and rendering in a filter and have a blank servlet that does nothing. The main difference is that if there is no servlet mapped against a given URL, the container must respond with a 404 error so there must always be a servlet mapped against any URL you want to serve. You can also only have one servlet mapped against a URL, but you can have any number of filters.