W3docs

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

To import the javax.servlet or jakarta.servlet API in an Eclipse project, follow these steps:

To import the javax.servlet or jakarta.servlet API in an Eclipse project, follow these steps:

Manual JAR Addition (Legacy Setup)

  1. Right-click on your project in the "Project Explorer" and select "Properties" from the context menu.
  2. In the "Properties" window, select "Java Build Path" from the list on the left and then click on the "Libraries" tab.
  3. Click on the "Add External JARs" button.
  4. In the "JAR Selection" window, navigate to the location of the servlet-api.jar file. This file is usually located in the "lib" folder of your Tomcat installation directory.
  5. Select the servlet-api.jar file and click "Open".
  6. Click "OK" to close the "Properties" window.

Modern Dependency Management (Recommended) For modern projects, use a build tool instead of manually adding JARs:

  • Maven: Add the following to your pom.xml:
    `<dependency>`
        `<groupId>`javax.servlet`</groupId>`
        `<artifactId>`javax.servlet-api`</artifactId>`
        `<version>`4.0.1`</version>`
        `<scope>`provided`</scope>`
    `</dependency>`
    For Jakarta EE 9+, use:
    `<dependency>`
        `<groupId>`jakarta.servlet`</groupId>`
        `<artifactId>`jakarta.servlet-api`</artifactId>`
        `<version>`6.0.0`</version>`
        `<scope>`provided`</scope>`
    `</dependency>`
  • Gradle: Add to your build.gradle:
    implementation 'javax.servlet:javax.servlet-api:4.0.1'
    // or for Jakarta EE 9+:
    implementation 'jakarta.servlet:jakarta.servlet-api:6.0.0'

Verify the Import Create a simple servlet class to verify the API is recognized:

import javax.servlet.http.HttpServlet; // or jakarta.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
    // Eclipse will resolve the imports automatically
}

You should now be able to import classes from the javax.servlet or jakarta.servlet package in your project.

Note: If you are using a version of Eclipse that is newer than 2018-12, you may need to use the jakarta.servlet API instead of the javax.servlet API, as the Java EE specification has been transferred to the Eclipse Foundation and the package names have been changed to use the "jakarta" namespace.