- Spring Boot - Home
- Spring Boot - Introduction
- Spring Boot - Quick Start using CLI
- Spring Boot - Bootstrapping
- Spring Tool Suite
- Spring Boot - Tomcat Deployment
- Spring Boot - Build Systems
- Spring Boot - Code Structure
- Spring Boot - Beans & Dependency Injection
- Spring Boot - Runners
- Spring Boot - Starters
- Spring Boot - Application Properties
- Spring Boot - Configuration
- Spring Boot - Annotations
- Spring Boot - Logging
- Building RESTful Web Services
- Spring Boot - Exception Handling
- Spring Boot - Interceptor
- Spring Boot - Servlet Filter
- Spring Boot - Tomcat Port Number
- Spring Boot - Rest Template
- Spring Boot - File Handling
- Spring Boot - Service Components
- Spring Boot - Thymeleaf
- Consuming RESTful Web Services
- Spring Boot - CORS Support
- Spring Boot - Internationalization
- Spring Boot - Scheduling
- Spring Boot - Enabling HTTPS
- Spring Boot - Eureka Server
- Service Registration with Eureka
- Gateway Proxy Server and Routing
- Spring Cloud Configuration Server
- Spring Cloud Configuration Client
- Spring Boot - Actuator
- Spring Boot - Admin Server
- Spring Boot - Admin Client
- Spring Boot - Enabling Swagger2
- Spring Boot - Using SpringDoc OpenAPI
- Spring Boot - Creating Docker Image
- Tracing Micro Service Logs
- Spring Boot - Flyway Database
- Spring Boot - Sending Email
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - Batch Service
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - Unit Testing
- Rest Controller Unit Testing
- Spring Boot - Database Handling
- Securing Web Applications
- Spring Boot - OAuth2 with JWT
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 Sign-In
Spring Boot Resources
Spring Boot - Servlet Filter
A filter is an object used to filter the HTTP requests and responses of your application. By using filter, we can perform two operations at two instances −
- Before sending the request to the controller
- Before sending a response to the client.
The following code shows the sample code for a Servlet Filter implementation class with @Component annotation.
@Component
public class SimpleFilter implements Filter {
@Override
public void destroy() {}
@Override
public void doFilter
(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {}
@Override
public void init(FilterConfig filterconfig) throws ServletException {}
}
Example - Usage of Filter
The following example shows the code for reading the remote host and remote address from the ServletRequest object before sending the request to the controller.
In doFilter() method, we have added the System.out.println statements to print the remote host and remote address.
SimpleFilter.java
package com.tutorialspoint.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 {}
}
In the Spring Boot main application class file, we have added the simple REST endpoint that returns the "Welcome to Tutorialspoint!" string.
DemoApplication.java
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping(value = "/")
public String hello() {
return "Welcome to Tutorialspoint!";
}
}
The code for Maven build pom.xml is given below −
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tutorialspoint</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The code for Gradle Build build.gradle is given below −
build.gradle
buildscript {
ext {
springBootVersion = '3.5.6'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
You can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands shown below −
For Maven, use the command as shown below −
mvn clean install
After BUILD SUCCESS, you can find the JAR file under the target directory.
For Gradle, use the command as shown below −
gradle clean build
After BUILD SUCCESSFUL, you can find the JAR file under the build/libs directory.
Now, run the JAR file by using the following command
java jar <JARFILE>
You can see the application has started on the Tomcat port 8080.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) [2025-09-28T16:32:58Z] [org.springframework.boot.StartupInfoLogger] [main] [53] [INFO ] Starting DemoApplication using Java 21.0.6 with PID 41312 (D:\Projects\demo\target\classes started by mahes in D:\Projects\demo) [2025-09-28T16:32:58Z] [org.springframework.boot.SpringApplication] [main] [652] [INFO ] No active profile set, falling back to 1 default profile: "default" [2025-09-28T16:32:59Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [111] [INFO ] Tomcat initialized with port 8080 (http) [2025-09-28T16:32:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Initializing ProtocolHandler ["http-nio-8080"] [2025-09-28T16:32:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting service [Tomcat] [2025-09-28T16:32:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting Servlet engine: [Apache Tomcat/10.1.46] [2025-09-28T16:32:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Initializing Spring embedded WebApplicationContext [2025-09-28T16:32:59Z] [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] [main] [301] [INFO ] Root WebApplicationContext: initialization completed in 1162 ms [2025-09-28T16:33:00Z] [org.apache.juli.logging.DirectJDKLog] [main] [168] [INFO ] Starting ProtocolHandler ["http-nio-8080"] [2025-09-28T16:33:00Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 8080 (http) with context path '/' [2025-09-28T16:33:00Z] [org.springframework.boot.StartupInfoLogger] [main] [59] [INFO ] Started DemoApplication in 2.177 seconds (process running for 2.722)
Now hit the URL http://localhost:8080/ and see the output Hello World. It should look as shown below −
Then, you can see the Remote host and Remote address on the console log as shown below −
Remote Host:0:0:0:0:0:0:0:1 Remote Address:0:0:0:0:0:0:0:1 Pre Handle method is Calling Post Handle method is Calling Request and Response is completed