Filters and interceptors are suitable for occasions



Filter, in java web, you pass in the request and response to filter out some information in advance, or set some parameters in advance, and then pass in the action of servlet or struts for business logic, such as filtering out illegal urls (not login. do address requests, if the user is not logged in, they will be filtered out), or uniformly set the character set before the action of the incoming servlet or struts, or remove some illegal characters

Interceptor, in aspect-oriented programming, is to call a method before your service or a method, or call a method after the method. For example, dynamic proxy is a simple implementation of interceptor, and print out a string before you call the method ( Or do other business logic operations), you can also print out a string after you call the method, or even do business logic operations when you throw an exception.

 

The difference between interceptor and filter :

  1. Interceptors are based on java's reflection mechanism, while filters are based on function callbacks.
  2. Interceptors do not depend on the servlet container, filters depend on the servlet container.
  3. Interceptors can only work on action requests, 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 action's life cycle, while filters can only be called once when the container is initialized

Execution order : before filtering - before intercepting - action processing - after intercepting - after filtering. I personally think that filtering is a horizontal process. First, filter the content submitted by the client (for example, the processing that users who are not logged in cannot access internal pages); after the filtering is passed, the interceptor will check the verification of the data submitted by the user, and do some preliminary data. Processing, and then send the processed data to the corresponding Action; after the Action processing is completed and returned, the interceptor can also do other processes (I haven't thought of what to do), and then return to the subsequent operations of the filter.

 

 Aspect-Oriented Programming (AOP is the acronym for Aspect Oriented Program) , we know that object-oriented features are inheritance, polymorphism and encapsulation. Encapsulation requires that functions be distributed into different objects, which is often called responsibility assignment in software design. In fact, that means having different classes design different methods. In this way, the code is scattered into the classes one by one. The advantage of this is to reduce the complexity of the code and make the class reusable.
      But people have also found that while decentralizing the code, it also increases the duplication of the code. What does that mean? For example, we may need to do logging in each method in two classes. According to 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 of object-oriented design that there is no connection between classes, and it is impossible to unify these duplicate codes.

    Some people may say, that's easy to handle, we can write this code in a separate method of a separate class, and then call it in these two classes. However, in this way, these two classes are coupled with the separate classes we mentioned above, and its changes will affect both classes. So, is there any way that we can add code at will when we need it? This kind of programming idea that dynamically cuts the code into the specified method and position of the class at runtime is aspect-oriented programming. 
      Generally speaking, the code fragments that we cut into the specified methods of the specified classes are called aspects, and which classes and methods we cut into are called entry points. With AOP, we can extract the code shared by several classes into a slice, and then cut into the object when needed, thereby changing its original behavior.
In this way, AOP is actually just a supplement to OOP. OOP distinguishes each class from the horizontal, while AOP adds specific code to the object vertically. With AOP, OOP becomes three-dimensional. If the time dimension is added, AOP makes OOP change from two-dimensional to three-dimensional, and from plane to three-dimensional. Technically, AOP is basically implemented through a proxy mechanism. 
     AOP can be said to be a milestone in the history of programming, and it is a very useful supplement to OOP programming.


Original link: http://blog.sina.com.cn/s/blog_8bcfeeda010107q0.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325822758&siteId=291194637
Recommended