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:
- The server intercepts the request and identifies the program to process it.
- The server invokes the program and provides the request data to it.
- The program processes the request and generates HTML contents.
- These contents are provided to the server by the program.
- 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:
- 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.
- 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?
- To remove the overhead of process creation & destruction, a thread based request processing model is provided by servlet.
- 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.