Servlet and filter differences and the execution order of Servlets, filter, Interceptor

Source: Internet
Author: User
Tags http request
1) The difference between servlet and filter
Filter can be considered as a "variant" of the servlet, which is mainly used to preprocess user requests or post-process httpservletresponse, which is a typical processing chain. It differs from the servlet in that it cannot generate a response directly to the user. The complete process is that the filter pre-processes the user request, then gives the request to the servlet for processing and generates a response, and finally the filter then processes the server response.

2) execution order of servlet, filter, Interceptor

As can be seen in the above diagram, the order should be that we match the filter in front, followed by the Struts2 Filterdispatcher, then each interceptor, and finally the original path back. cwx714 (architect) 2010-05-10

Filter and servlet:
Http://www.cnblogs.com/Fskjb/archive/2010/03/27/1698448.html

Filter and Struts interceptors
http://bruceyoo.iteye.com/blog/631355
http://jwx0925.iteye.com/blog/631968

The order of execution of interceptors and filters in Struts2 proves:
http://blog.163.com/mail_sv/blog/static/13853926420102295392416/

==============
Slightly more detailed reading Anddy (architect) 2010-05-11 What's the difference between referencing a servlet and filter

See Servelt specification:
Reference a filter is a reusable piece of code that can transform the content of the HTTP requests, responses, and header information . Filters don't generally create a response or respond to a request as servlets do, rather they modify or adapt the request s for a resource, and modify or adapt responses from a resource.

Under brief translation:
Filter is a reusable code fragment that can be used to transform HTTP requests, responses, and header information. Filter is not like a servlet, it cannot produce a request or response, it simply modifies a request to a resource, or modifies a response from one.

Then look at the inheritance system of filter (do not inherit the Servlet interface):

Java Code public Interface Filter

So the first conclusion: Servlet is not related to filter. But 2 of them can work together very well.


Reference
Servlet, filter, Interceptor

First, about interceptor. Interceptor is the concept introduced in Xwork, the core of Struts2 is XWORK2. Struts2 Dispatcherfilter from the Serlvetcontainer, gets to the HTTP request, and then to the internal interceptor processing.

Interceptor is not a concept in a servlet JSR. So the order in which interceptor and other filter executes depends on the order in which Struts2 's dispatcherfilter and other filter are executed.

The problem becomes: the execution order of the dispatcherfilter of the Servlet Filter Struts2.

The order in which the servlet and filter is executed is illustrated in the servlet JSR:

If a request matches a serlvet, and the servlet is configured with one or more filter, there is no doubt that the filter is executed before the servlet is executed.

For the filter with multiple matches, which is executed first?

The JSR illustrates that, according to multiple matching filter, it is executed in the order that it is configured in Web. Xml.

So that's why you put your filter or other filter (like Urlrewrite filter) in front of struts ' dispatcherfilter. Because, they need to do some pre-work before the request is processed by the STRUTS2 framework.

When filter is called and enters the dispatcherfilter of Struts2, STRUTS2 will follow the interceptor order in the interceptor stack configured in action. To invoke Interceptor.


Xiaolongfeixiang (senior architect) 2010-05-11 you can actually think of interceptor as the method of the Struts filter call. You'll be clear about that.

This process is the same as the previous filter-> Servlet.
Xiaolongfeixiang (senior architect) 2010-05-11

Reference Org.apache.struts.action.ActionServlet

This is the most central servlet in struts, and all requests are through it. Anddy (architect) 2010-05-11 look at this picture, and then look at this article, and you should understand the relationship between servlet and action. The action simply handles the business logic, not the servlet. Actionservlet is the controller in the struts, responsible for all the forwarding, allocation processing, etc. at the bottom.



http://cheng5259.iteye.com/blog/638716 Anddy (architect) 2010-05-11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.