Struts2 filter and interceptor

Source: Internet
Author: User

When learning struts2, I found that there are filters and interceptors. They all seem to have the same functions, but why are there two different names? There must be a difference, so I plan to sort it out by myself.

Filter: in Java Web, the request and response you pass in filter out some information in advance, or set some parameters in advance, and then input the servlet or struts action for business logic, for example, filter out invalid URLs (not login. do address requests are filtered out if the user does not log in), or the character set is set before the servlet or struts action is passed in, or some invalid characters are removed.

 

Aspect-oriented programming (AOP is the abbreviation of Aspect Oriented Program)We know that object-oriented features inheritance, polymorphism, and encapsulation. Encapsulation requires that functions be distributed to different objects, which is often called Responsibility Assignment in software design. In fact, let different classes design different methods. In this way, the code is dispersed into classes. This reduces the complexity of the code and makes the class reusable.
However, it is also found that code duplication is also increased while code is scattered. What does it mean? For example, we may need to log in every method in two classes. Based on the object-oriented design method, we must add the log content to the methods of both classes. Maybe they are exactly the same, but it is because the object-oriented design makes the Class and Class unable to be linked, rather than unifying the repeated code.
Some people may say that it is easy to do. We can write this code in an independent class independent method and then call it in these two classes. However, in this way, these two classes are coupled with the independent classes we mentioned above, and their changes will affect these two classes. Is there any way to add code as needed? During the runtime, the idea of dynamically cutting the code into the specified method and position of the class is the Aspect-Oriented Programming.
Generally, the code snippet that is used to specify a method in a specified class is called a cut-in. The cut-in class and method are called a cut-in point. With AOP, we can extract the code of several classes into a single slice, and then cut into the object when necessary to change its original behavior.
In this case, AOP is actually just a supplement to OOP. Oop separates classes horizontally, while AOP adds specific code to objects vertically. With AOP, oop becomes three-dimensional. With the time dimension added, AOP changes Oop from the original two-dimensional to the three-dimensional, from the plane to the three-dimensional. Technically, AOP is basically implemented through the proxy mechanism.
AOP is a milestone in the history of programming, and it is a very useful supplement to OOP programming.

 

The Interceptor is programmed in the aspect-oriented way by calling a method before your service or method, or calling a method after the method. For example, a dynamic proxy is a simple implementation of the interceptor, you can print a string (or perform other operations on the business logic) before calling the method. You can also print a string after calling the method, even when you throw an exception, you can perform business logic operations.

 

Differences between interceptor and filter:

  1. The Interceptor is based on the Java reflection mechanism, and the filter is based on function callback.
  2. The interceptor does not depend on the servlet container, and the filter depends on the servlet container.
  3. The interceptor can only work on Action requests, while the filter can work on almost all requests.
  4. The interceptor can access objects in the Action context and value stack, but the filter cannot.
  5. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.

Execution sequence: Before filtering-before intercepting-action processing-after intercepting-after filtering. I personally think that filtering is a horizontal process. First, the content submitted by the client is filtered out (for example, the user who is not logged on cannot access the internal page). After filtering, the interceptor checks the data submitted by the user, performs some preliminary data processing, and then sends the processed data to the corresponding action. After the action is processed and returned, the interceptor can also perform other processes (not expected to do anything), and then return to the filter for subsequent operations.

Struts2 filter and interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.