Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 872
CuriousMind Avatar asked Aug 12 '14 05:08

CuriousMind


People also ask

Are servlets Java EE?

well, JSP and servlets are themselves part of Java EE.

What is the use of servlet API jar?

Servlet API provides all the required interfaces, classes, and methods to develop a web application. we need to add the servlet-api. jar file in our web application to use the servlet functionality. We can download this jar file from Maven Repository.

What is servlet and servlet container?

What Are Servlets and Their Containers. Servlets are a component of the JEE framework used for web development. They are basically Java programs that run inside the boundaries of a container. On the whole, they are responsible for accepting a request, processing it, and sending a response back.

What JAR files are required for servlets?

You need Servlet-api. jar to compile servlets in eclipse but while deploying servlet container ( like tomcat ) will have it built in.


3 Answers

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.

like image 83
icza Avatar answered Oct 22 '22 20:10

icza


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.

like image 33
Luiggi Mendoza Avatar answered Oct 22 '22 20:10

Luiggi Mendoza


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.

like image 44
jcool Avatar answered Oct 22 '22 19:10

jcool