Summarize the difference between Interceptor and Filter

I. Introduction

The difference between Interceptor and Filter is a question that is often asked in interviews, and it is also a question that is easily confused by everyone in development. In this summary, I hope it will be helpful to everyone.

2. Introduction of Filter

2.1. Concept

The three technologies Servlet, Listener, and Filter in the Servlet specification (the order is LFS).

Filter is a new function added after servlet2.3 in sun company. An interface javax.servlet.Filter is defined in javaEE to describe the filter.

2.2. Function

The request and response operations for accessing web resources can be intercepted through Filter.

WEB developers use Filter technology to intercept all web resources managed by the web server: such as Jsp, Servlet, static image files or static html files, etc., so as to realize some special functions. For example, it implements some advanced functions such as URL-level permission access control, filtering sensitive words, and compressing response information.

2.3. Examples

In java web, filter out some information in advance for the incoming request or response, or set some parameters in advance, and then pass it into the servlet for business logic, such as filtering out illegal urls (not login.do address requests, if the user If there is no login, filter it out), or uniformly set the character set before passing in the servlet or struts action, or remove some illegal characters.

2.4. Introduction to Filter API

Filter is an interface under the javax.servlet package, which mainly has the following three methods:

destory()

doFilter(ServletRequest request,ServletResponse response,FilterCjain chain)

init(FilterConfig filterConfig)

2.5, Filter chain and Filter life cycle

1) Introduction of Filter chain

Multiple Filters intercept the same resource, so when we execute chain.doFilter(request, response) in the initial Filter, we access the next Filter until the last Filter is executed, and there is no Filter behind it. Will access web resources.

2) The access sequence problem of multiple FIlters

Their execution order depends on the order of configuration in the web.xml file.

3) Filter life cycle

When the server starts, a Filter object is created and the init method is called, only once.

When accessing resources, the path matches the interception path of the Filter, and the doFilter method in the Filter will be executed,
which is the method for actually intercepting the operation.

When the server is shut down, the destroy method of Filter will be called to perform the destruction operation.

The filter is a JavaEE standard, and it is implemented in the way of function callback. It is pre-processing after the request enters the container and before it enters the Servlet, and post-processing is performed after the request is returned to the front end.

3. Introduction to Interceptor

The interceptor belongs to the spring container and is supported by spring.

An interceptor in java is an object that dynamically intercepts Action calls. It provides a mechanism that allows developers to define the code to be executed before and after an action is executed, or to prevent an action from executing before it is executed, and also provides a way to extract reusable parts of an action.

In aspect-oriented programming AOP (Aspect-Oriented Programming), the interceptor is used to intercept a method or field before it is accessed, and then add some operations before or after it.

4. Summarize the difference between Filter and Interceptor

1. Different implementation principles

Interceptors are based on java's reflection mechanism, while filters are based on function callbacks.

2. The scope of use is different

The filter implements the javax.servlet.Filter interface, and this interface is defined in the Servlet specification, that is to say, the use of the filter depends on containers such as Tomcat, so it can only be used in web programs.

Interceptor (Interceptor) It is a Spring component and is managed by the Spring container. It does not depend on containers such as Tomcat and can be used alone. It can be used not only in web programs, but also in programs such as Application and Swing.

3. Different trigger timing

Filter Filter is preprocessed after the request enters the container, but before entering the servlet, and the end of the request is after the servlet is processed.

The Interceptor is preprocessed after the request enters the servlet and before entering the Controller, and the request ends after the corresponding view is rendered in the Controller.

insert image description here

4. Interceptors can access objects in the action context and value stack, but filters cannot.

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

6. The interceptor can obtain each bean in the IOC container, but the filter cannot. This is very important. A service can be injected into the interceptor to call the business logic. Interceptors can obtain service beans in ioc to implement business logic.

Guess you like

Origin blog.csdn.net/KevinChen2019/article/details/127537209