Java Interceptor vs Filter

Source: Internet
Author: User

The implementation principle of interceptors:

The Interceptor method is called by proxy. The block implementation of Struts 2 is relatively straightforward. When the request reaches the servletdispatcher of Struts 2, struts 2 looks for the configuration file, instantiates the relative interceptor object according to its configuration, and then strings a list to form an interceptor chain, and the last to invoke the interceptor in the list.

The fundamentals of servlet filters
When the servlet is used as a filter, it can process the client's request. When processing is complete, it is handed over to the next filter, so that the customer's request is processed one by one in the filter chain until the request is sent to the target. When the filter is processed successfully, the submitted data is sent to the final target, and if the filter processing is unsuccessful, the view is distributed to the specified error page.

The difference between interceptors and filters:
1. Interceptors are based on the Java reflection mechanism, and the filter is based on function callbacks.
2. The interceptor is not dependent on the servlet container, and the filter relies on the servlet container.
3. Interceptors only work on action requests, while filters can work on almost all requests.
4. The interceptor can access the action context, the object in the value stack, and the filter cannot be accessed.
5. The interceptor can be called multiple times during the life cycle of the action, and the filter can only be called once when the container is initialized

Java Interceptor vs Filter

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.