The difference between STRUTS2 filter and Interceptor is analyzed in this paper. Share to everyone for your reference, specific as follows:
One, the essential difference:
1. The interceptor is based on the Java reflection mechanism, and the filter is based on a function callback.
2. The interceptor does not depend on the servlet container, and the filter relies on the servlet container.
3. Interceptors can only function on action requests, and filters work on almost any request.
4. The interceptor can access the action context, the object in the value stack, and the filter is inaccessible.
5. In the life cycle of the action, the interceptor can be invoked multiple times, and the filter can only be invoked once when the container is initialized
Second, the use of the difference:
The 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.
For example, filter out illegal URLs (not login.do address requests, if the user does not login are filtered out),
or set the character set uniformly before passing in the servlet or struts action.
Or get rid of some illegal characters (often used in chat rooms, some curse words) ...
The interceptor can pass the action that matches the condition. The interceptor itself is an ordinary Java object that can dynamically intercept action calls,
Execute the various kinds of Web project requirements that the interceptor itself provides before and after the action executes. You can also block the execution of the action, and you can also extract
The part of the action that can be reused.
(It's in the aspect-oriented programming, calling a method before your service or a method, or calling a method after the method, such as a dynamic proxy, is a simple implementation of the Interceptor, printing a string (or doing other business logic operations) before you call the method, You can also print out a string after you call the method, and even do business logic when you throw an exception. )
More interested readers of struts-related content can view this site: the introduction and advanced course of the struts framework, the Spring Framework introduction and advanced tutorials, and the Hibernate framework introduction and advanced tutorials.
I hope this article will help you with the Java programming based on the struts framework.