'how to add the servlet api to my pom.xml

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?



Solution 1:[1]

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

Solution 2:[2]

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>

Solution 3:[3]

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>

Solution 4:[4]

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.

Solution 5:[5]

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.

Solution 6:[6]

Jakarta EE

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.

Servlet 5

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>

Servlet 6

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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neil
Solution 2 Adam
Solution 3
Solution 4 Vineet Reynolds
Solution 5 TheCodeArtist
Solution 6