Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the responsibility chain model and how Filter works?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the chain of responsibility model and the working principle of Filter. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In every software engineer's dictionary, there are a lot of high-end words, which may be different because of the differences between the development language and the environment, but many of them go beyond the language.

As musicians often say

Music knows no national boundaries.

For software engineers, both JAVA engineers and C # engineers will understand design patterns.

Design pattern has no development language boundaries, and it is a design method extracted from object-oriented experience to solve the problem of repetition.

There are many classic works about design patterns, such as Gof's design patterns, like Robert.C.Martin 's

P.P.P . On the other hand, there are not many books on design patterns in open source projects. I'm going to write a series of articles that analyze the use of design patterns in Tomcat. This article is the first to introduce the use of the chain of responsibility pattern in Tomcat.

Introduction

Chain of Refponsibility (chain of responsibility) is a mode of object behavior.

In Gof's book Design patterns, the intent for this pattern is described as follows:

It gives multiple objects the opportunity to process the request, thus avoiding the coupling relationship between the sender and receiver of the request. Concatenate the object into a chain and pass the request along the chain until an object processes it.

For the understanding of this pattern, the most important thing is to understand that it has been passing down an object. Yan Hong once described the pattern by beating drums and passing flowers. In Web applications, this passing flower is a request.

Now that we understand the pattern, let's look at how this pattern is implemented in Tomcat.

In the use of Filter, we will notice that its interface contains such a method

Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)

We see that in this method, in addition to passing down the please request and response, another FilterChain object is passed. After each logical processing of the current Filter, we usually call the doFilter method of the FilterChain to execute the logic of the next Filter. It should be noted that it is easy to misunderstand that the interface method of this FilterChain is also called doFilter. In fact, the declaration of this method is as follows:

/ * *

* Causes the next filter in the chain to be invoked, or if the calling

* filter is the last filter in the chain, causes the resource at the end of

* the chain to be invoked.

, /

Public void doFilter (ServletRequest request, ServletResponse response)

It might be easier to understand if you call it invokeFilter or executeFilter here.

So, each time, after the execution of a Filter, how does the doFilter of the FilterChain confirm who will execute the next Filter?

The secret is here, that is, the implementation of the FilterChain interface.

/ * *

* Invoke the next filter in this chain, passing the specified request

* and response. If there are no more filters in this chain, invoke

* the service () method of the servlet itself.

*

, /

Public void doFilter (ServletRequest request, ServletResponse response) {

If (Globals.IS_SECURITY_ENABLED) {/ / Note that SecurityManager is enabled here. For more information, you can reply the keyword 003 at the backend to view it.

Final ServletRequest req = request

Final ServletResponse res = response

Try {

Java.security.AccessController.doPrivileged (

New java.security.PrivilegedExceptionAction () {

@ Override

Public Void run ()

Throws ServletException, IOException {

InternalDoFilter (req,res)

Return null

})

} catch (PrivilegedActionException pe) {}

} else {

InternalDoFilter (request,response); / / the default processing method for each call is here.

}}

Private void internalDoFilter (ServletRequest request

ServletResponse response) {

/ / Call the next filter if there is one

If (pos < n) {

ApplicationFilterConfig filterConfig = filters [pos++]

Filter filter = null

Filter = filterConfig.getFilter ()

Filter.doFilter (request, response, this)

Return

}

/ / We fell off the end of the chain-- call the servlet instance

If ((request instanceof HttpServletRequest) & &

(response instanceof HttpServletResponse)) {

Servlet.service (request, response)

} else {

Servlet.service (request, response)

}}

We can see that in the control that really deals with FilterChain, it is judged by the two parameters pos and n. The statements of the two are as follows:

/ * *

* The int which is used to maintain the current position

* in the filter chain.

, /

Private int pos = 0

/ * *

* The int which gives the current number of filters in the chain.

, /

Private int n = 0

We see that one identifies the Filter currently being processed, and one identifies the total number of filter in the chain.

After all the Filter in the chain is executed, the logic of pos < n no longer executes, skipping to the real Servlet request processing that follows.

On the other hand, each Filter can be preprocessed and post-processed before and after filterChain execution. Because each filter call is finally similar to a recursive call, there will be a return operation in the logic after the doFilter of the filter is executed, which ensures that it will not be executed down to the logic of the Servlet.

Understand the implementation principle of Filter and the basic concept of the responsibility chain pattern, we only need to ensure that each chain implements the same interface, and every time the Chain is passed in the interface method, it can be well implemented. In the logic that controls the execution of Chain, you can store the elements of the entire chain through an array or List, like Tomcat:

/ * *

* Filters.

, /

Private ApplicationFilterConfig [] filters =

New ApplicationFilterConfig [0]

In control, each time the next element is taken out for execution, the chain of responsibility can be implemented.

After reading the above, do you have any further understanding of the chain of responsibility model and how Filter works? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report