What Is a Website Blocker?
Interceptors in Java are objects that dynamically intercept Action calls. It provides a mechanism that allows developers to define code that executes before and after an action is executed, and prevents it from executing before an action is executed. It also provides a way to extract reusable parts of an action. In AOP (Aspect-Oriented Programming), an interceptor is used to intercept a method or field before it is accessed and then add some operations before or after.
- Chinese name
- Interceptor
- Foreign name
- Interceptor
- Classification
- java
- Interceptors in Java are objects that dynamically intercept Action calls. It provides a mechanism that allows developers to define code that executes before and after an action is executed, and prevents it from executing before an action is executed. It also provides a way to extract reusable parts of an action. In AOP (Aspect-Oriented Programming), an interceptor is used to intercept a method or field before it is accessed and then add some operations before or after.
Interceptor Principle
- Most of the time, interceptor methods are called through a proxy. Struts 2's interceptor implementation is relatively simple. When the request reaches Struts 2's ServletDispatcher, Struts 2 will look up the configuration file, instantiate the corresponding interceptor object according to its configuration, then string it into a list, and finally call the interceptors in the list one by one. The Struts2 interceptor is pluggable, and the interceptor is an implementation of AOP. The Struts2 interceptor stack is to link the interceptors into a chain in a certain order. When accessing intercepted methods or fields, the interceptors in the Struts2 interceptor chain are called in the order they were previously defined.
Interceptor defines an interceptor
- Customizing an interceptor requires three steps:
- 1. Customize a class that implements the Interceptor interface (or inherits from AbstractInterceptor).
- 2. Register the interceptor defined in the previous step in struts.xml.
- 3. Refer to the interceptor defined above in the Action to be used. For convenience, the interceptor can also be defined as the default interceptor, so that all actions are intercepted by this interceptor without special declaration. [1]
The difference between interceptors and filters
- A filter can be simply understood as "take what you want" and ignore things you don't want; an interceptor can be simply understood as "reject what you want to reject" and care about what you want to reject, such as a BBS forum Block sensitive words.
- 1. Interceptors are based on the Java reflection mechanism, while filters are based on function callbacks.
- 2. The filter depends on the servlet container, and the interceptor does not depend on the servlet container.
- 3. Interceptors only work on actions, while filters can work on almost all requests.
- 4. Interceptors can access objects in the action context and value stack, while filters cannot.
- 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.