The implementation principle of interceptors:
The Interceptor method is called by proxy. The block implementation of Struts 2 is relatively straightforward. When the request reaches the servletdispatcher of Struts 2, struts 2 looks for the configuration file, instantiates the relative interceptor object according to its configuration, and then strings a list to form an interceptor chain, and the last to invoke the interceptor in the list.
The fundamentals of servlet filters
When the servlet is used as a filter, it can process the client's request. When processing is complete, it is handed over to the next filter, so that the customer's request is processed one by one in the filter chain until the request is sent to the target. When the filter is processed successfully, the submitted data is sent to the final target, and if the filter processing is unsuccessful, the view is distributed to the specified error page.
The difference between interceptors and filters:
1. Interceptors are based on the Java reflection mechanism, and the filter is based on function callbacks.
2. The interceptor is 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. The interceptor can be called multiple times during the life cycle of the action, and the filter can only be called once when the container is initialized
Java Interceptor vs Filter