Introduction To Servlets

What is a Servlet ?

            A servlet is a java technology based Web component, managed by a container, that generates dynamic content. Servlets run on the servers, hence is the name Servlets. Like other Java technology-based components, servlets are platform - independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology - enabled Web server. Containers, sometimes called servlet engines, are Web server extensions that provide servlet functionality. Servlets interact with Web clients via a request / response paradigm implemented by the servlet container.

What is a Servlet Container ?

        The servlet container is a part of a Web server or application server that provides the network services over which requests and responses are sent, decodes MIME - based requests, and formats MIME - based responses. A servlet container also contains and manages servlets through their life cycle.

A servlet container can be built into a host Web server, or installed as an add-on component to a Web server via that server's native extension API. Servlet containers can also be built into or possibly installed into Web-enabled application servers.

All servlet containers must support HTTP as a protocol for requests and responses, but additional request / response - based protocols such as HTTPS (HTTP over SSL) may be supported. The required versions of the HTTP specification that a container must implement are HTTP / 1.0 and HTTP / 1.1 . It may modify requests from the clients before delivering them to the servlet, may modify responses produced by servlets before sending them to the clients, or may resond to requests without delivering them to the servlet under the compliance with RFC2616 (HTTP / 1.1) .

A servlet container may place security restrictions on the environment in which a servlet executes. In a J2SE (Java Platform, Standard Edition ) or J2EE (Java EE) environment , these restrictions should be placed using the permission architecture defined by the Java platform. For example, high - end application servers may limit the creation of a Thread object to insure that other components of the container are not negatively impacted.

An Example Describing How Servlets Work : 
  1. A client ( e.g. , a Web browser) accesses a Web server and makes an HTTP request. 
  2. The request is received by the Web server and handed off to the servlet container. The servlet container can be running in the same process as the host Web server, in a different process on the same host, or on a different host from the Web server for which it processes requests. 
  3. The servlet container determines which servlet to invoke based on the configuration of its servlets, and calls it with objects representing the request and response.
  4. The servlet uses the request object to find out who the remote user is, what HTTP POST parameters may have been sent as part of this request, and other relevant data. The servlet performs what ever logic it was programmed with, and generates data to send back to the client. It sends this data back to the client via the response object. 
  5. Once the servlet has finished processing the request, the servlet container ensures that the response is properly flushed, and returns control back to the host Web server. 
Comparing Servlets With Other Technologies :

In functionality, servlets lie some where between Common Gateway Interface (CGI) programs and proprietary server extensions such as the Netscape Server API (NSAPI) or Apache Modules.

Servlets have the following advantages over other server extension mechanisms :
  1. They are generally much faster than CGI scripts because a different process model is used. 
  2. They use a standard API that is supported by many Web servers. 
  3. They have all the advantages of the Java programming language, including ease of development and platform independence. 
  4. They can access the large set of APIs available for the Java platform.