Java EE Servlet/filter/phase监听器区别及处理顺序
我想知道处理请求的顺序是什么,servlet(@WebServlet)、过滤器(@WebFilter)、阶段侦听器等之间有什么区别。
这些方法具有非常相似的标头(doGet/doFiler)。
I want to know what the order of processing requests is and what's the difference between servlet (@WebServlet), filter (@WebFilter), phase listeners etc.
These methods have very similar headers (doGet/doFiler).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里描述了与过滤有关的请求处理以及随后由 servlet 进行的处理:
http://download.oracle.com/docs/cd/B32110_01/web.1013/b28959/filters.htm
因此,您主要使用 servlet 来传递内容并可能更改请求/使用过滤器进行响应。过滤器可用于实现管道和过滤器或装饰器设计模式。 (尽管它们也可以自己传递内容,并且根本不会将委托转发给 servlet 进行最终处理。)
servlet 请求处理就这么多。 PhaseListeners 是一个更高抽象级别的概念。它们不属于 servlet 规范,而是属于构建在 servlet 之上的 Java Server Faces 概念。它们可用于跟踪 JSF 组件在请求期间经历的阶段,因此当您想要在请求期间影响 JSF 组件的行为/呈现时,它们是过滤器的替代方案。
可以在此处找到使用阶段侦听器的小示例:
http://www.softwareengineeringsolutions.com/thoughts/frameworks/JSF.Techniques -PhaseListeners.htm
The processing of a request in regard to filtering and then processing by a servlet is described here:
http://download.oracle.com/docs/cd/B32110_01/web.1013/b28959/filters.htm
Thus you'd mainly use a servlet do deliver content and maybe alter request/response using filters. Filters can be used to implement a pipes and filters or a decorator design pattern. (Though they can also deliver content by themselves and don't forward delegation to final processing by a servlet at all.)
So much to the servlet request processing. PhaseListeners are a higher abstraction level concept. They don't belong to the servlet spec but to the Java Server Faces Concepts building ontop of servlets. They can be used to track the phases your JSF Components go through during a request and thus are the alternative for filters when you want to influence behaviour/rendering of JSF components during a request.
A litte example for the usage of phase listeners can be found here:
http://www.softwareengineeringsolutions.com/thoughts/frameworks/JSF.Techniques-PhaseListeners.htm