Java
Eclipse IDE
Programming
Jakarta Servlet API
Web Development

How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When working with Java web applications in Eclipse, you will often require access to the javax.servlet API (Servlet API). Recent updates and evolutions in the Java ecosystem have led to the introduction of the jakarta.servlet API as part of the migration from the Java EE (Enterprise Edition) platform to the Jakarta EE platform. This guide is designed to explain how to import the required servlet APIs into your Eclipse project.

Understanding Servlet APIs

Servlets are Java programs that run on a web server; they are designed to handle requests from web clients, typically browsers, and respond to them. To develop servlets, you need to have the servlet API in your classpath. This API provides the necessary interfaces like HttpServletRequest and HttpServletResponse, and classes like HttpServlet.

Configuring Eclipse to Use Servlet APIs

To start working with servlets in Eclipse, follow these steps to import the servlet APIs:

Step 1: Create a Dynamic Web Project

  1. Open Eclipse IDE and go to the "File" menu.
  2. Select "New" and then "Dynamic Web Project".
  3. Enter a project name, and ensure the target runtime is set (Apache Tomcat, Jetty, etc.).
  4. Click "Finish".

Step 2: Ensure Java EE Libraries Are Included

Eclipse should automatically include Java EE libraries (which contain the javax.servlet API) if the project is set up as a Dynamic Web Project and the correct target runtime is selected. If the libraries are not showing up:

  1. Right-click the project in the Project Explorer.
  2. Go to "Properties" -> "Project Facets".
  3. Make sure "Dynamic Web Module" and "Java" facets are checked and are set to appropriate versions.
  4. Click "Apply and Close".

Step 3: Add Libraries Manually (if required)

If for any reason the Java EE libraries are not included, or if you specifically need to include the jakarta.servlet API, follow these steps:

  1. Download a compatible library (e.g., Jakarta EE Web Profile).
  2. Right-click the project and select "Build Path" -> "Configure Build Path".
  3. Under the "Libraries" tab, click "Add External JARs...".
  4. Select the downloaded JAR files and click "Open", then "Apply and Close".

Using Maven or Gradle

For projects managed through Maven or Gradle, you can add dependencies for the servlet APIs in your pom.xml or build.gradle file respectively.

Maven:

xml
1<dependencies>
2    <dependency>
3        <groupId>jakarta.servlet</groupId>
4        <artifactId>jakarta.servlet-api</artifactId>
5        <version>5.0.0</version>
6        <scope>provided</scope>
7    </dependency>
8</dependencies>

Gradle:

groovy
dependencies {
    providedCompile 'jakarta.servlet:jakarta.servlet-api:5.0.0'
}

Transition from javax.servlet to jakarta.servlet

The transition from javax to jakarta namespaces is part of the migration from Java EE to Jakarta EE. While the APIs are largely compatible, some changes may require adjustments in your code.

Key Points

AspectDetails
API Libraryjavax.servlet or jakarta.servlet
Automatic InclusionTypically included in Dynamic Web Projects with a proper runtime
Manual AdditionPossible through IDE or build tools (Maven/Gradle)
Namespace TransitionFrom javax to jakarta
Typical Project Setup RequirementJava EE Libraries or Jakarta EE Web Profile libraries

Conclusion

Including the Servlet API in your Eclipse project is straightforward, especially when using a Dynamic Web Project setup. For advanced configuration, managing dependencies through Maven or Gradle offers a robust alternative. Remember that the migration to jakarta.servlet might require updates to your existing Java EE applications.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design