spring-boot-3
ClassNotFoundException
javax.servlet
HttpServletRequest
Java

Spring Boot 3 javax.servlet HttpServletRequest ClassNotFoundException fix

Codemia Knowledge Hub ·

Spring Boot 3 throws ClassNotFoundException for javax.servlet.http.HttpServletRequest because of the Jakarta migration. Boot 3 is built on Spring Framework 6, which moved from the javax.servlet namespace to jakarta.servlet, so you need jakarta.servlet.http.HttpServletRequest in your code and in every library on the classpath.

javax.servlet became jakarta.servlet

Before Spring Boot 3, servlet APIs came from packages such as javax.servlet.http. Jakarta EE 9 renamed them to jakarta.servlet.http. The classes are conceptually the same, but the package names differ, so bytecode that references javax.servlet.http.HttpServletRequest can no longer resolve in a Boot 3 application.

Any of these can trigger the exception:

  • your own imports still use javax.servlet.*
  • a filter, interceptor, or controller library was compiled against the old servlet API
  • a third-party starter pulls in pre-Jakarta dependencies
  • generated code or copied examples still reference javax

Fixing your own imports

Controller and filter classes in Boot 3 must import the Jakarta servlet types.

java
1package example.web;
2
3import jakarta.servlet.http.HttpServletRequest;
4import org.springframework.web.bind.annotation.GetMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7@RestController
8public class DemoController {
9
10    @GetMapping("/agent")
11    public String agent(HttpServletRequest request) {
12        return request.getHeader("User-Agent");
13    }
14}

If a class still imports javax.servlet.http.HttpServletRequest, it compiles only when old dependencies are present, which usually means the project is half-migrated.

Finding old servlet dependencies

The error can remain after you fix your imports if an older library still depends on javax.servlet. Authentication filters, servlet listeners and older Spring extensions are common culprits.

In Maven, inspect the dependency tree:

bash
mvn dependency:tree | grep servlet

In Gradle:

bash
./gradlew dependencies --configuration runtimeClasspath | grep servlet

Look for outdated libraries that still bring in javax.servlet-api or older Spring components. In a Boot 3 app, everything servlet-related should use Jakarta packages.

If you declare the servlet API yourself, make it the Jakarta one:

xml
1<dependency>
2    <groupId>jakarta.servlet</groupId>
3    <artifactId>jakarta.servlet-api</artifactId>
4    <scope>provided</scope>
5</dependency>

Most Spring Boot applications do not need this dependency directly, because the Boot starters manage it. Adding both the old and new servlet APIs only creates confusion.

Why it shows up during a 2.x to 3.x upgrade

The error is common during upgrades from Spring Boot 2.x to 3.x because most of the codebase looks compatible at first. Controllers, services and repositories often migrate cleanly, while servlet-related imports fail later, or only at runtime through reflective loading.

Filters are a typical place to miss. A migrated filter must use Jakarta imports throughout:

java
1import jakarta.servlet.FilterChain;
2import jakarta.servlet.ServletException;
3import jakarta.servlet.http.HttpServletRequest;
4import jakarta.servlet.http.HttpServletResponse;
5import org.springframework.web.filter.OncePerRequestFilter;
6
7public class RequestLoggingFilter extends OncePerRequestFilter {
8
9    @Override
10    protected void doFilterInternal(
11            HttpServletRequest request,
12            HttpServletResponse response,
13            FilterChain filterChain) throws ServletException, java.io.IOException {
14        filterChain.doFilter(request, response);
15    }
16}

A single leftover javax import is enough to break it.

Mistakes that keep the error around

  • Fixing only your own code. If a third-party library was compiled against javax.servlet, you need a Jakarta-compatible version of that library.
  • Adding both javax.servlet-api and jakarta.servlet-api. This does not create compatibility. It causes classpath ambiguity and hides the real problem.
  • Assuming Tomcat is missing. In most Boot 3 applications the embedded server is fine. The problem is application or library bytecode that references the wrong package.
  • Following Boot 2.x documentation. Security filters, custom servlet components and container customizers often need import changes plus dependency upgrades.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms