Difference between javax.servlet-api.jar vs servlet-api.jar
Asked Answered
P

5

33

In my maven repository under groupId javax.servlet i have these two separate artifacts for servlets. I am confused which one should i use to build a simple servlet application? What's the difference between these two artifacts?

Perrin answered 18/12, 2015 at 5:27 Comment(2)
What are the artifact names you have?Nannettenanni
I have javax.servlet-api and servlet-api. Which one is which?Perrin
M
14

javax.servlet-api version 3.0.1 has annotation folder which contains different annotation classes where servlet-api version 2.5 or below (i.e version 2.4) does not contain annotation.

Annotation represents the metadata. If you use annotation, deployment descriptor i.e. web.xml is not required. For example if you use annotation like @WebServlet("/hello") in your servlet file then you don't need to mention servlet mapping in web.xml file.

Some of useful Annotations are:

@HandlesTypes
@HttpConstraint 
@HttpMethodConstraint
@MultipartConfig
@ServletSecurity
@WebFilter
@WebInitParam
@WebListener
@WebServlet
Misjudge answered 20/10, 2016 at 10:52 Comment(0)
N
7

You need to add

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

to your project. The version you need may differ - it depends on your servlet container, e.g. Tomcat.

<scope>provided</scope> because you don't need it in runtime, it's already in your servlet container.

Nannettenanni answered 18/12, 2015 at 5:41 Comment(4)
So you mean the purpose of javax.servlet is for my development only? Is it like jdk and jre analogy for javax.servlet-api and servlet-api ?Perrin
Yes, it's for development only, so your code could be successfully compiled. In runtime your servlet container will provide necessary functionality.Nannettenanni
You haven't told the difference between them and why to use one over the other :(Spacetime
The question is "What's the difference between these two artifacts?"Longlived
D
6

If you have to deploy on an ancient application server version that doesn't support the servlet 3.0 spec (hopefully unlikely), stick with the old servlet-api.

With the 3.0 spec, they moved it over to javax.servlet-api. See: https://javaee.github.io/servlet-spec/

Now, with the move of Java EE from Oracle to the Eclipse Foundation (Jakarta EE), the spec has again moved. If at all possible you may want to consider using the new group and artifact if you want to stay up-to-date: jakarta.servlet:jakarta.servlet-api

https://github.com/eclipse-ee4j/servlet-api

Decca answered 18/6, 2019 at 18:58 Comment(0)
W
4

Go with javax.servlet-api.jar , Many developers mistakenly include servlet-api.jar in their WEB-INF/lib folder. This no longer causes an exception because Tomcat and other app servers will recognize it as a problem when deploying the JAR file. However, it does cause the container to ignore any JAR file that contains the javax/servlet/Servlet.class.

Wadleigh answered 18/12, 2015 at 5:39 Comment(0)
E
1

For those using gradle...

If I declare my dependency using compileOnly as below

compileOnly "javax.servlet:javax.servlet-api:3.1.0"

then I get a compilation error:

error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
                         ^

If I use providedCompile as below the build is successful.

providedCompile "javax.servlet:javax.servlet-api:3.1.0"

To use providedCompile dependencies you need to use the war plugin.

apply plugin: 'war'
Erotogenic answered 28/8, 2018 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.