How do I add the servlets API to my project's pom.xml
mvnrepository.com has lots of servlet api and similarly named projects, that I don't know which is the right one. Or are all of them ok?
How do I add the servlets API to my project's pom.xml
mvnrepository.com has lots of servlet api and similarly named projects, that I don't know which is the right one. Or are all of them ok?
I believe most web/app servers come bundled with a version of the servlet api, so you won't want to bundle the api in your .war file. You will need to find out which version is included with your server, then you can use
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api-version}</version>
<scope>provided</scope>
</dependency>
replacing servlet-api-version with your version. You will want to specify the "provided" scope so the api.jar isn't included in your war file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
For servlet-api 3.1.0, here is the declaration :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
We use
<dependency>
<groupId>javax</groupId>
<artifactId>j2ee</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
but if you only need the servlet api you might want to use
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>?</version>
<scope>provided</scope>
</dependency>
Scope provided can be used when you dont want to put jar file inside the WEB-INF/lib
folder instead you are supplying it at runtime either by container or JDK.
It depends on which version of the servlet API you are using.
The javax.servlet artifact will provide jars for all of the servlet API versions.
In recent years, Oracle transferred the Java EE technologies to the Eclipse Foundation. There the technologies have been renamed to Jakarta EE. So Java Servlet is now known as Jakarta Servlet.
This name change was done to respect Oracle’s trademarks. Do a Web search to find many articles and videos discussing this transition.
This name change includes changing the package naming of the classes from javax.*
to jakarta.*
. This is a breaking change, though updating your project may be as simple as merely changing your import
statements. But check that any libraries you depend on have versions available using the new naming as well.
This transition has brought new versions of the Servlet specification. Version 5 of the spec is the same as Servlet 4 but with the new naming.
For the current version, edit your POM file to use the following Maven dependency. Check for updates in a Maven repository of your choice in the version numbering.
You can deploy web apps built with Servlet 5 to web containers such as Tomcat 10.0.x, Jetty 11.0.x, Glassfish 6, and several more.
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
Jakarta Servlet 6 specification is currently in development, and will contain significant changes. The spec will be finalized later this year 2022.
See the overview page, the product page, project links page, and repository coordinates page.
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>