Analysis of the difference between struts2 filters and interceptors

  • 2020-05-07 19:51:42
  • OfStack

This paper analyzes the differences between struts2 filters and interceptors. Share with you for your reference, as follows:

1. Essential difference:

1. The interceptor is based on java's reflection mechanism, while the filter is based on function callbacks.

2. The interceptor does not depend on the servlet container, and the filter depends on the servlet container.

3. The interceptor only works on action requests, while the filter works on almost all requests.

4. Interceptors can access objects in the action context, value stack, and filters cannot.

5. In the lifetime of action, the interceptor can be invoked multiple times, while the filter can only be invoked once when the container is initialized

2. Use distinction:

The filter is in java web. You pass request,response filters out some information in advance, or sets some parameters in advance, and then passes servlet or action of struts for business logic.
For example, filter out illegal url (not login.do address requests, if the user is not logged in),
Or set the character set before passing servlet or struts action 1,
Or remove 1 illegal character (often used in chat rooms)...

The interceptor can pass through the qualified action. The interceptor itself is a normal Java object that dynamically intercepts Action calls,

Before and after the execution of Action the various sample Web project requirements provided by the interceptor itself. You can also block the execution of Action and extract it
Reusable parts of Action.

(is in aspect oriented programming, which is in your service or a method before calling a method, or call a method after the method, such as dynamic proxy is the simple implementation interceptors, before you call a method to print out a string (or do other business logic operation), also can print out the string after you call a method, even when you throw an exception for the operation of the business logic.)

For more information about Struts, please see the topics on this site: Struts, Spring, and Hibernate.

I hope this article is helpful for you to design Java program based on Struts framework.


Related articles: