This paper analyzes the difference between struts2 filter and interceptor. Share it for your reference, as follows:
1. Essential differences:
1. Interceptors are based on Java's reflection mechanism, while filters are based on function callbacks.
2. Interceptor does not depend on servlet containers, filters depend on servlet containers.
3. Interceptors can only work on action requests, while filters can work on almost all requests.
4. The interceptor can access objects in the action context and value stack, but the filter cannot.
5. During the life cycle of an action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized.
2. Differences in use:
The filter is in the java web. You pass in the request and response to filter out some information in advance, or set some parameters in advance, and then pass in the servlet or struts action for business logic.
For example, filter out illegal urls (not login.do address requests, if the user does not log in, filter out them),
Or set the character set before passing in the servlet or struts action.
Or remove some illegal characters (often used in chat rooms, some swearing words). . .
The interceptor can pass an action that meets the criteria. The interceptor itself is a normal Java object, which can dynamically intercept Action calls.
The execution of the interceptor itself provides various web project requirements before and after the action is executed. It can also prevent the execution of Action, and it can also be extracted.
The part that can be reused in Action.
(It is programmed in a tangential direction, which means calling a method before your service or a method, or calling a method after the method. For example, a dynamic proxy is a simple implementation of an interceptor. It prints out a string before you call the method (or does other business logic operations), or prints out a string after you call the method, or even does business logic operations when you throw an exception.)
For more information about Struts, readers who are interested in this site can view the topics: "Struts Framework Introduction and Advanced Tutorial", "Spring Framework Introduction and Advanced Tutorial" and "Hibernate Framework Introduction and Advanced Tutorial".
I hope that the description in this article will be helpful to everyone's Java programming based on the Struts framework.