image
Intercepting Filter
Start course
Difficulty
Intermediate
Duration
4h 56m
Students
116
Ratings
4/5
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello dear Friends. In this video, we will examine the intercepting filter pattern. Let's begin. The intercepting filter design pattern is used when we want to do some pre-processing or post-processing with 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 pluggable 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 requests to corresponding handlers. Intercepting filters are filters that trigger actions before or after an incoming request is processed by a handler affecting existing handlers. Pre-processing and post-processing 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 check aborting the request. Nested if-else statements are a standard strategy but this solution leads to code fragility and to copy and paste style of programming because the flow of the filtering and the action of the filters is compiled into 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 interception filter pattern. There are four participants. The filter will perform certain tasks prior or after execution of request 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. Now, let's use this pattern in a project. Create a new project and name it intercepting filter. 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, and execute method. By defining a filter chain, we can add multiple filters to intercept a request. 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. 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 add filter method. Let's create request method. Done. So who will use this manager? Of course, we will 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. See you in the next video.

 

About the Author
Students
3900
Courses
64
Learning Paths
5

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.

Covered Topics