The difference between a filter and an interceptor

Source: Internet
Author: User

1, interceptors are based on the reflection mechanism of Java, and filter is based on function callback

2. The filter relies on the servlet container, and the interceptor does not depend on the servlet container

3.interceptors only work on action requests, while filters can work on almost all requests

4. Theinterceptor can access the action context, the object in the value stack, and the filter cannot

5, in the life cycle of the action , the interceptor can be called multiple times, and the filter can only be called once when the container is initialized

  Interception Device : It is in the aspect programming that you call a method before your service or a method, or call a method after a method such as a dynamic proxy is a simple implementation of the Interceptor, before you call the method to print out the string (or do other business logic operations), You can also print out a string after you invoke the method, even if you do business logic when you throw an exception.

filter : Yes inJava Web, you pass in theRequest,responsefilter out some information in advance, or set some parameters in advance, and then pass inservletorStrutsof theActionconduct business logic, such as filtering out illegalURL(Notlogin.doaddress request, if the user does not log in are filtered out),or in the incomingservletorStrutsof theActionset the character set before, or remove some illegal characters.

using filters to filter the JSP pages in the/admin directory , first Filter configuration in Web. XML :

The difference between a filter and an interceptor

Related Article

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.