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.
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
- Open Eclipse IDE and go to the "File" menu.
- Select "New" and then "Dynamic Web Project".
- Enter a project name, and ensure the target runtime is set (Apache Tomcat, Jetty, etc.).
- 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:
- Right-click the project in the Project Explorer.
- Go to "Properties" -> "Project Facets".
- Make sure "Dynamic Web Module" and "Java" facets are checked and are set to appropriate versions.
- 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:
- Download a compatible library (e.g., Jakarta EE Web Profile).
- Right-click the project and select "Build Path" -> "Configure Build Path".
- Under the "Libraries" tab, click "Add External JARs...".
- 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:
Gradle:
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
| Aspect | Details |
| API Library | javax.servlet or jakarta.servlet |
| Automatic Inclusion | Typically included in Dynamic Web Projects with a proper runtime |
| Manual Addition | Possible through IDE or build tools (Maven/Gradle) |
| Namespace Transition | From javax to jakarta |
| Typical Project Setup Requirement | Java 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.
Related reading
- How do I invoke a Java method when given the method name as a string?
- How do I load an HTTP URL with App Transport Security enabled in iOS 9?
- How do I make a large number of concurrent HTTPS requests robustly in Clojure /Java
- How do I make calls to a REST API using C?
- How do I initialize a byte array in Java?
- How do I install Java on Mac OSX allowing version switching?
- How do I mock a REST template exchange?
- How do I modify proxy responses using asynchronous servlets?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.