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?

- 4,389
- 4
- 36
- 54

- 2,209
- 3
- 19
- 33
-
What are the artifact names you have? – dds Dec 18 '15 at 05:33
-
1I have javax.servlet-api and servlet-api. Which one is which? – Ruelos Joel Dec 18 '15 at 05:35
5 Answers
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

- 186
- 2
- 14
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.

- 2,335
- 1
- 31
- 45
-
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 ? – Ruelos Joel Dec 18 '15 at 05:47
-
Yes, it's for development only, so your code could be successfully compiled. In runtime your servlet container will provide necessary functionality. – dds Dec 18 '15 at 05:49
-
11You haven't told the difference between them and why to use one over the other :( – Kanagavelu Sugumar Aug 22 '17 at 04:37
-
The question is "What's the difference between these two artifacts?" – slugmandrew Jun 27 '19 at 10:41
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.

- 184
- 1
- 11
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

- 303,325
- 100
- 852
- 1,154

- 115
- 6
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'

- 13,270
- 8
- 79
- 72