'Which servlet-api jar to use
My application needs to be deployed in tomcat 8 server in production . In production the application will use the servlet-api.jar which comes with tomcat 8. So in development which of the following servlet jars should we be using ? Are they both the same?
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.20</version>
<scope>provided</scope>
</dependency>
Solution 1:[1]
javax jars are specifications. But since they are both provided-scoped, I'm guessing you don't need either while deploying.
From Maven docs:
providedThis is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
If you need to use anything from the javax specification, chances are your framework already includes this jar in its dependencies. As long as you don't get any runtime exceptions about unknown classes during development, I wouldn't worry about that too much.
Solution 2:[2]
Oracle/Java provides specifications in form of Interfaces/Signatures and some basic rules and leaves implementations for vendors/container providers e.g. Tomcat and weblogic and so on...
Now in your case since you are going to deploy it on tomcat so you should stick with servlet-Jar provided by tomcat.
Solution 3:[3]
Why not download Tomcat 8.0.20 and have a look? On the other hand this page show Tomcat 8 uses Servlet 3.1.x.
Solution 4:[4]
I prefer to use dependencies that match the tomcat version you are using, as you mentioned:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.20</version>
<scope>provided</scope>
</dependency>
Although the two dependencies you mentioned are almost the same, there are subtle differences between them. For example, the source code in the two jars have different descriptions, which makes the line number of the compiled class files different, resulting in breakpoints doesn't work during debugging.
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 | Czyzby |
| Solution 2 | mds404 |
| Solution 3 | Stefan |
| Solution 4 | Poison |
