20
votes

I need understanding about the serlvet-api.jar which is needed to compile a servlet.

I am building a simple servlet, like this:

import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {
    // Remaining code here
}

I know that we need servlet-api.jar file to compile this simple servlet, like this:

javac -classpath /path/where/jar/is/servlet-api.jar

Now my doubts starts here:

  1. What is servlet-api.jar?
  2. Who provides this jar?
  3. Does each web-container provide this jar e.g., Tomcat, Jboss, glassfish? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet.
  4. When we download Java EE , is this jar part of download? OR do we get this file as part of web container?
  5. Consider this situation:

    Suppose we compile / build the simple servlet using Tomcat (i.e tomcat's version of jar needed to build the servlet) and create a .war file. Can we then deploy the war in some other vendor container?

4

4 Answers

18
votes

What is it?

The servlet-api jar is a library which contains the interfaces and classes of the Servlet API specification. The servlet-api jar contains only the interface (the API) of the Servlet Specification, so you can use it to develop your web application.

Where can you get it?

It is provided at the link below:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Where it is contained/bundled

Servlet-api.jar is part of the Java EE download so you can develop your web applications (you could not compile your FirstServlet class if the Java EE would not contain it).

Servlet containers (like Tomcat, JBoss, GlassFish etc.) also contain the servlet-api.jar else they would not be able to run your web application, and moreover they also contain the implementation of the interfaces that are part of the Servlet API.

The name is not always the same though, and it might not even exist as a separate jar, the Servlet API classes might be bundled in another jar.

You can however download a separate jar file containing only the Servlet API if you just want to develop a web application for a Servlet container, or if you want to create/write your own Servlet API implementation. Look at here:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Portability

You can compile your web application if you have the Servlet API, no matter where it comes from. After you compiled your web app, you can optionally pack it into a WAR file (WAR=Web ARchive) which is simply a zip file containing your static files, your compiled java classes and configuration files like web.xml etc. And you will be able to run your compiled web application in any Servlet containers (but read forward).

So answer to your question #5 is:

There are multiple versions of the Servlet API, and there are more to the Java EE platform than just the Servlet API (e.g. Enterprise Java Beans). But it's safe to say that if you only use the Servlet API, all Servlet containers that implement that version of the Servlet API will be able to run your web application.

The configuration files of the different web applications might differ though (which is outside of the Servlet API scope), so you should always check the documentation of the target web application.

3
votes

What is servlet-api.jar?

It is a jar that provides the necessary interfaces/classes to write Servlets.

Who provides this jar?

Any servlet container such as Jetty or Tomcat and any Java EE compliant application server like JBoss/Wildfly, GlassFish, IBM WebSphere, Oracle WebLogic, etc.

Does each web-container provide this jar e.g., Tomcat, Jboss, glassfish? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet.

Yes for the servlet api because it is a set of interfaces, which enables programming to interfaces rather than class implementations so we avoid programming to a specific application server. The name of the jar containing the implementation must not have part of this name in common.

When we download Java EE , is this jar part of download? OR do we get this file as part of web container?

Java EE download available in Oracle is just GlassFish. This is covered previously.

Suppose we compile / build the simple servlet using Tomcat (i.e tomcat's version of jar needed to build the servlet) and create a .war file. Can we then deploy the war in some other vendor container?

As long as you don't use a Tomcat specific class, library or feature, then yes, there would be no problem. Otherwise, no.

1
votes

It is the server container vendor which needs to provide this servlet-api you can find it in this directory tree C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib (it depends on your container download location). And of course it is provided by other containers.

0
votes

A lot of things can easily be checked based on POM files. For example, Tomcat Catalina has a reference to tomcat-servlet-api(newer version of org.apache.tomcat:servlet-api)

Notice also, and it can cause some confusion when investigating dependencies trees, that Tomcat Embed doesn't use any servlet-api jar at all and instead just specifies Servlet API contract inside itself.