Intercepting Filter Design Pattern
Start course
3h 59m

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine the Intercepting Filter Pattern. So, let's begin. The Intercepting Filter Design Pattern is used when we want to do some preprocessing or postprocessing with the request or response of the application. Filters are defined and applied on the request before passing the request to the actual target application. Intercepting filter is a Java EE pattern which creates pluckable filters to process common services in a standard manner without requiring changes to core request processing code. Filters can do the authentication, authorization, logging, or tracking of requests, and then pass the request to corresponding handlers. Intercepting filters are filters that trigger actions before or after an incoming request is processed by a handler, affecting existing handlers.

Preprocessing and postprocessing of a request refer to actions taken before and after the core processing of that request. Some of these actions determine whether processing will continue, while others manipulate the incoming or outgoing data stream into a form suitable for further processing. The classic solution consists of a series of conditional checks with any failed checker boarding the request. Nested if else statements are a standard strategy. But this solution leads to code fragility, and a copy and paste style of programming, because the flow of the filtering and the action of the filters is compiled as the application. The key to solving this problem in a flexible and unobtrusive manner is to have a simple mechanism for adding and removing processing components in which each component completes a specific filtering action. As you can see in the UML of the intercepting filter pattern, the filter will perform certain tasks prior or after execution of requests by request handler.

The filter manager manages filter processing. It creates the filter chain with the appropriate filters in the correct order and initiates processing. Filter chain carries multiple filters and helps to execute them in defined order on target. The target is the resource requested by the client. I think you got it. Now, let's use this pattern in a project. Create a new project and name it, InterceptingFilter. Let's start off with defining an interface for the filter. We need an execute() method. Now, we will create concrete implementations, authentication filter. Override execute() method. Go on with debugging filter. Override. Now, we will define the target of the request. Add execute() method. By defining a filter chain, we can add multiple filters to intercept a request. Let's define first filter. We need filter List and target. First, we will add filter() method. Let's add execute() method. Also, we have to set target to the chain.

Okay, done. We now need a Manager class to help manage this filter chain. First, define filter chain. Now in constructor, we will add this as parameter. We need to add filter() method. Let's create request method. Okay, done. So, who will use this Manager? Of course, we need a client. We need two methods, set filter manager, and send request. Now, we can use this pattern. First, define filter Manager. Now, add filters. Let's create an instance from client and set Manager. Send a request. Now, let's run. As you can see, the request has been put through both filters from the filter chain before being forwarded to the target. We have now finished examining the Intercepting Filter Pattern. In the following video, we will look at the final design pattern for this course, the Front Controller Pattern. So, I'll see you in the following video my friend.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.