Spring Boot Servlet Filter

Spring Boot Servlet Filters

In this article, I am going to discuss Spring Boot Servlet Filters with Examples. Please read our previous article where we discussed Spring Boot Interceptor.

What is a Servlet Filter in Spring Boot?

A servlet filter is an object that is used to intercept HTTP requests. In Spring Boot, servlet filters can execute in three situations:

  • Before sending a request to the controller.
  • Before sending a response to the client.
How Servlet Filter is different from Interceptors in Spring Boot?

Servlet filters are part of the web server and not specific to the Spring Framework/Spring Boot. Filters are used to manipulate and block requests. Also, they can stop responses from reaching the client. Interceptors cannot perform these tasks.

How to Add Servlet Filters in Spring Boot?

To add servlet filters to Spring Boot, only one class is required, which will be used to place the filter code. For this exercise, we shall use the hello world project from our previous exercises.

Step 1: Create the servlet filter code file. Create a file called in SimpleFilter.java file in the src/main/java/com/dotnet/demo directory.

Step 2: Import the following packages into the class:

How to Add Servlet Filters in Spring Boot?

Step 3: Modify the code as follows:

How Servlet Filter is different from Interceptors in Spring Boot?

We have performed the following modifications:

  • Added @Component annotation to the FruitServiceInterceptor class.
  • Implemented Filter interface in the SimpleFilter class.
  • Added code for destroy(), doFilter(), and init() functions.

In this case, we have left destroy() and init() functions empty. We have written code in the doFilter function. This code shall print out the name and IP address of the remote host.

Step 4: Execute the application. Ensure that compilation is successful.

What is a Servlet Filter in Spring Boot?

Step 5: Access the application by opening the http://localhost:8080 URL in your web browser. The “Hello World!” text should be visible.

Spring Boot Servlet Filters

Now, go back to the terminal. You will notice that the address of the web server that accessed this page is printed:

Spring Boot Servlet Filters with Examples

Refresh the page, a new request shall be sent, and another address will print:

Spring Boot Servlet Filters with Examples

Congratulations! You have now learned how to add servlet filters to Spring Boot!

The Complete Example Code
SimpleFilter.java
package com.dotnet.demo;

import java.io.IOException;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;

import org.springframework.stereotype.Component;

@Component
public class SimpleFilter implements Filter
{
    @Override
    public void destroy()
    {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
    throws IOException, ServletException
    {
        System.out.println("Remote Host:"+request.getRemoteHost());
        System.out.println("Remote Address:"+request.getRemoteAddr());
        filterchain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig)
    throws ServletException
    {
        
    }
}
DemoApplication.java
package com.dotnet.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication
{
    
    public static void main(String[] args)
    {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping(value = "/")
    public String hello ()
    {
        return "Hello World!";
    }
}

In the next article, I am going to discuss Spring Boot File Handling with Examples. Here, in this article, I try to explain Spring Boot Servlet Filters with Examples. I hope you enjoy this Spring Boot Servlet Filters article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

Your email address will not be published. Required fields are marked *