what is a servlet?

In this post, you will learn:

  • What is a servlet?
  • What is servlet API?
  • What is CGI?
  • What are the limitations of CGI?
  • How servlet API removes the limitations of CGI?

The term Servlet has two different meaning in two different contexts. In the broader context, it represents an API of dynamic web application development and in the narrow context, it represent a Java class which is defined using this API for processing requests in a web application.

As API, Servlet contains interfaces and classes which are used to create web applications in Java. Implementation of some of the these interfaces is provided by the web server vendors and implementation of the rest is provided by the application programmers. These interfaces define a standard mode of communication between the server and the Servlets (request processing programs). To understand the need of such interfaces, we need to understand how a request for dynamic contents is processed by the server.

A request for dynamic contents has following work flow:

  1. The server intercepts the request and identifies the program to process it.
  2. The server invokes the program and provides the request data to it.
  3. The program processes the request and generates HTML contents.
  4. These contents are provided to the server by the program.
  5. The server sends these contents as response to the client.

i.e. A web server and a request processing program need to collaborate with each other in the processing of a request. For this collaboration there must be some standard rules of communication b/w the server and the program. Before Servlet these rules were provided by CGI. You must have heard this term before? No! don’t worry, lets find out what it is?

Common Gateway Interface (CGI) is a protocol which provides standard rules for servers to invoke programs, to provide request data to them, and to receive processing result from them. In CGI based web applications programs were written in C, C++ or perl. These programs were called CGI Scripts. you would be thinking that when we had CGI, why servlet was introduced?

CGI based web applications had following drawbacks:

  1. For each request, a procees is started by the server to execute the CGI script. Process creation and destruction, used to result in more overhead than actual request processing.
  1. CGI Scripts were platform dependent i.e. if a web application is to be deployed from Windows to Linux server or vice-versa, CGI scripts need to be recompiled for the target platform.

To remove these drawbacks of CGI based web applications, Sevlet API was introduced by sun Microsystems in 1998. Now, you will be wondering, how Servlets removes these drawbacks?

  1. To remove the overhead of process creation & destruction, a thread based request processing model is provided by servlet.
  2. By facilitating the development of web applications in Java, problem of platform dependency is removed.

The role of the API, is to facilitate communication between a web server and a request processing program (henceforth called Servlet). For this Sun Microsystems defined a set of interfaces, representing standard rules for defining Servlets, instantiating Servlets, processing requests,  and transferring data between the server and the servlets etc.

javax.servlet.Servlet is the main interface of the API, it provides methods which define initialization, processing and destruction phase of a Servlet. These methods are called servlet life cycle methods.

Application programmers are told by Sun Microsystems that they need to define classes which implements this interface i.e. application programmer are asked to define methods which are expected by the server.

A web server is told that application programmers will provide Sevlet classes to it as part of an application i.e. the server will need to invoke these methods for request processing.

By providing same information to both the parties Sun Microsystems facilitated communication between the web servers and servlets in a standard manner.

you have covered a lot of ground, don’t worry if you haven’t grasped concepts such as thread based request processing model, they are covered in depth in the later posts.

Its time to recap the main concepts:

  1. Request processing is a team work. A web server and a request processing program need to work together to process a request.
  2. For this team work, standard rules are required which need to be followed by both the parties.
  3. CGI, is the first set for rules for this team work. Programs which follow these rules are called CGI Scripts.
  4. Servlet API, is another set of rules, provided by Sun Microsystems, for this team work in Java.
  5. In Java, the program which process requests are called Servlets.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *