The difference between interceptors and filters:

Source: Internet
Author: User

    1. Interceptors are based on the reflection mechanism of Java, and filters are based on function callbacks.
    2. Interceptors are 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. Interceptors can be called multiple times during the life cycle of an action, and filters can only be called once when the container is initialized

Execution Order : Pre-filter-before-intercept-action handling-after interception-after filtering. Individuals think that filtering is a horizontal process, the first to filter the content submitted by the client (for example, the user does not have access to the internal page processing); After filtering, the interceptor will check the validation of user submitted data, do some pre-processing, and then send the processed data to the corresponding action After the action process is returned, the interceptor can also do other procedures (what is not expected to be done), and then return up to the filter for subsequent operations.

Filter, is in the Java Web, your incoming request,response filter out some information in advance, or set some parameters in advance, and then pass in the servlet or struts action for business logic, such as filtering out illegal URLs (not login.do address requests, if the user does not log on to filter out), or in the servlet or struts before the action of the uniform set of character sets, or to remove some illegal characters

Interceptors, which are programmed in aspect-oriented, call a method before your service or a method, or invoke a method after a method such as a dynamic proxy is a simple implementation of the Interceptor, print out the string before you call the method (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.

A filter is responsible for intercepting multiple requests or responses: one request or response can also be intercepted by multiple requests.

Creating a filter takes only two steps:
(1) Create a filter processing class:

(2) Configure filter in the Web. xml file.
The Create Filter must implement the Javax.servlet.Filter interface, which defines three methods in the interface.
void init (filterconfig config): Used to complete the initialization of the filter.
void Destroy (): To complete the collection of some resources before the filter is destroyed.
void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain): Implements the filtering function, which is the additional processing added to each request and response.

Filter filters also have a life cycle: init ()->dofilter ()->destroy (), driven by the filter element in the deployment file. In servlet2.4, the filter can also be used to request the dispatcher, but must be declared in Web. XML, <dispatcher>include or forward or request or error</dispatcher> The element is in filter-mapping.

The difference between interceptors and filters:

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.