What are filters and interceptors, what are their differences, and what is the relationship with AOP?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[what are filters and interceptors, what are their differences, and what is their relationship with AOP?]

title:

[small java class of Xiuzhen academy] what are filters and interceptors, what are their differences, and what is their relationship with AOP?

Opening remarks:

Hello, I'm Zhang Quanliang, a student of the 12th phase of Zhengzhou branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points in deep thinking - what is filter, what is interceptor, what's their difference, and what's the relationship with AOP?

1. Background:

What is filter

Filter: depends on servlet container. In implementation, it can filter almost all requests based on function callback,

A filter instance can only be called once when the container is initialized

What is an interceptor

It depends on the web framework. In spring MVC, it depends on the spring MVC framework.

In terms of implementation, the reflection mechanism based on Java belongs to an application of aspect oriented programming (AOP),

It is to call a method before a method, or to call a method after the method.

What is AOP

AOP (aspect oriented programming) is a programming idea, which is a supplement to OOP

Common implementations: filter and interceptor (proxy mode)

OOP: there is nothing you can do about some public behaviors that exist in all objects

In order to reduce duplicate code, AOP (aspect oriented) technology was born

2. Knowledge analysis:

2.1 use of filter:

(1) Intercept the HttpServletRequest of the customer before the HttpServletRequest arrives at the servlet

(2) Check HttpServletRequest as needed, or modify HttpServletRequest header and data

(3) Check the httpservletresponse as needed, and modify the httpservletresponse header and data

2.2 create a filter

(1) Create filter processing class

Init method, destroy method, dofilter method

(2) On the web Configure filter in XML file

2.3 use of interceptor

By implementing the handlerinterceptor interface or inheriting the handlerinterceptoradapter abstract class,

Copy prehandle(), posthandle() and aftercompletion() to intercept the user's request

3. Frequently asked questions:

3.1 difference between filter and interceptor

(1) Different interface definitions

The filter interface is defined in javax In the servlet package, the interface handlerinterceptor is defined in org springframework. web. Servlet package

(2) Different specifications

Filter is defined in servlet specification and supported by servlet container; Interceptors are in the spring container and supported by the spring framework

(3) Use different resources

Interceptor is a spring component managed by spring, so it can use any resources in spring

Object; Filter cannot use spring container resources

(4) Different depth

Filter only works before and after the servlet; Interceptors can go deep into the front and back of methods, before and after exception throwing, etc.

(5) Different callers

Filter is called by the server; Interceptor is called by spring.

Therefore, filter always takes precedence over interceptor

3.2 execution sequence of multiple filters

(1) Chain. In the first filter: Part before dofilter()

(2) Chain. In the second filter: Part before dofilter()

(3) : business logic

(4) Chain. In the second filter: After dofilter()

(5) Chain. In the first filter: After dofilter()

3.3 section oriented programming

(1) Notification (enhanced) advice: what features do you want

(2) Join point: spring allows you to be a place for advice

Spring method, when AspectJ supports constructor or attribute injection

(3) Pointcut: defines the pointcut based on the join point

(4) Aspect: combination of notification and pointcut

(5) Introduction: add a new method property to an existing class

(6) Weaving: the process of applying facets to the target object to create a new proxy object

4. Coding practice:

5. Expand thinking:

5.1 code execution sequence of multiple filters and interceptors

The filter starts to execute after the servlet is started, enters the page access and starts to execute the interceptor

5.2 AOP implementation, unified exception

The focus is on the definition of pointcut. Exceptions can be expected exceptions and unexpected exceptions, which are handled in different ways

5.2 listener

To monitor the context information, servlet request information and servlet session information of the web application, i.e. ServletContext, ServletRequest and httpsession

Execution order: listener > Filter > interceptor

6. References:

Use of filter and filterchain:

(1) https://blog.csdn.net/zhaozheng7758/article/details/6105749

monitor:

(2) https://www.cnblogs.com/dudududu/p/8507735.html

Filter, interceptor and AOP implementation:

(3) https://blog.csdn.net/FU250/article/details/80292293

7. More discussion:

Q1: what are the categories of listeners?

A1: listener listeners based on object servlets include three types:

Servletcontextlistener listens for ServletContext objects

HttpSessionListener listens to the session object

Httprequestlistener listens to the request object

By listening event type:

An event listener that listens to the creation and destruction of domain objects themselves.

An event listener used to listen for the addition and deletion of properties of domain objects.

An event listener that listens to the state of an object bound to the httpsession domain.

Q2: the execution process of the filter and the order of calling the interface

A2: if there are multiple filters, the initialization order of the filters is web Configure from bottom to top in XML;

Note: the initialization sequence when the container is started is from bottom to top, but when users access resources;

The public void dofilter method of filter replication is from top to bottom according to the web The order of configuration in XML is executed from top to bottom.

Q3: what is the main function of the listener?

A3: count the number of people online and use httpsessionliser; Load initialization information: use servletcontextlistener;

Statistics of website visits to achieve access monitoring

8. Acknowledgement:

9. Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>