- Interceptors are based on the reflection mechanism of Java, and filters are based on function callbacks.
- Interceptors are not dependent on the servlet container, and the filter relies on the servlet container.
- Interceptors only work on action requests, while filters can work on almost all requests.
- The interceptor can access the action context, the object in the value stack, and the filter cannot be accessed.
- 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: