You can import the javax.servlet / jakarta.servlet API into an Eclipse project in three ways, depending on the project structure you are using. You can import it using Maven (pom.xml), Gradle (build.gradle), or manually by adding a JAR file.
In this blog, we will discuss how to import the javax.servlet / jakarta.servlet API in your Eclipse project.
Table of Contents:
What are Servlet APIs?
These are the Java classes used to create dynamic web applications, handle HTTP client requests, and generate responses on the server side. The javax.servlet package provides the usage of web applications.
Key Components of the Servlet API are:
- Servlet Interface: It is the base interface for servlets. Most servlets extend HttpServlet, which indirectly implements this interface.
- HttpServlet: It is a subclass of GenericServlet that provides HTTP functionality and handles responses.
- ServletRequest: It is used for the encapsulation of the client’s request and provides access to request parameters, headers, and other information.
- ServletResponse: It is used for the encapsulation of the servlet’s response, and allows the servlet to send the data back to the client.
- ServletConfig: It provides the information about a servlet.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
Methods to Import the Servlet API in Eclipse
There are 3 methods in which you can import the javax.servlet / jakarta.servlet in your Eclipse IDE. These are:
Method 1: Using Maven
Step 1: Open pom.xml
- In Eclipse, go to your project in the Project Explorer.
- Find the pom.xml file under the root of your project.
- Double-click to open it.
Step 2: Add the Dependency
- Inside <dependencies>, add the following code based on your project:
For javax.servlet (Java EE 8 and earlier)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
For jakarta.servlet (Jakarta EE 9 and later)
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
Step 3: Update Maven
- Save the pom.xml file (Ctrl + S).
- Right-click your project > Maven > Update Project (Alt + F5).
Method 2: Manually adding a JAR file
Step 1: Download the JAR file.
Step 2: Add the JAR file to Eclipse
- Open Eclipse and go to your project
- Right-click the project and select Build Path > Configure Build Path.
- In the Libraries tab, click Add External JARs.
- Browse to the downloaded .jar file and select Open.
- Click Apply and Close.
Method 3: Using Gradle
Step 1: Open build.gradle File
- In Eclipse, go to your Project Explorer.
- Locate and open the build.gradle file inside your project.
Step 2: Add the Dependency
- For javax.servlet (Java EE 8 and earlier)
dependencies {
implementation 'javax.servlet:javax.servlet-api:4.0.1'
}
- For jakarta.servlet (Jakarta EE 9 and later)
dependencies {
implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0'
}
Unlock Your Future in Java
Start Your Java Journey for Free Today
Note: If the application is running on the server like Tomcat, you can change it to providedCompile for Gradle 6 and compileOnly for Gradle 7+.
Step 3: Save the Gradle Project
- Save the build.gradle file (Ctrl + S).
- Then refresh it
- In Eclipse, go to Gradle Tasks (Gradle Projects View).
- Right-click on your project and select Refresh Gradle Project.
- Or you can run gradle build in the terminal.
Note: Use javax.servlet for Java EE 8 or below, and jakarta.servlet for Jakarta EE 9 or above.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
To add javax.servlet / jakarta.servlet API in Eclipse, you can do it in three ways. First, if you use Maven, add the dependency in the pom.xml file and update the project. Second, if you are using Gradle, add the dependency in the build.gradle file and then refresh the project. Thirdly, you can manually add the JAR file by downloading it and adding it to the build path in Eclipse.
To know more about this topic, you can refer to our Java Course.
How to Import javax.servlet / jakarta.servlet API in Eclipse Project – FAQs
Q1. How to add javax servlet in Eclipse?
Right-click on your project -> properties -> build path. Add to your build path jar file(s) that have the javax. servlet implementation.
Q2. What is the difference between Java servlet and Jakarta servlet?
Java Servlet (javax.servlet) was used in Java EE (up to version 8), while Jakarta Servlet (jakarta.servlet) is used in Jakarta EE (version 9 and later) after the transition from Oracle to the Eclipse Foundation.
Q3. What is the Jakarta Servlet package?
The jakarta. servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
Q4. What is the use of Javax servlet API?
It is used to build dynamic web pages that can interact with users & databases
Q5. What is the full form of JSP?
JSP stands for JavaServer Pages. It is a technology that allows developers to create web pages with dynamic content. JSP is an extension of Servlets.