CuriousMind CuriousMind - 1 year ago 79
Java Question

Understanding who provides servlet-api.jar, is it web-container or part of Java EE download

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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download