by Dinesh Thakur

Java servlets provide web developers with a simple consistent mechanism for extending the functionality of a web server and provide dynamic behaviour for web applications. Servlets are Java classes that function like CGI programs. They accept a request from a client (usually a web browser), process that request and return a response to the client. All servlets are loaded and executed by a servlet container that can run by itself or as a component of a web server. The servlet container is also referred to as a servlet engine in the early days of servlet technology.

Java applications are WORA (Write Once Run Any where). Java web applications are WODA application (Write Once Deploye Any where). Servlets is a Single Instance and Multiple threads principle base server side technology to develop server side components (A reusable java object is called as java “component”) as web resource programs of web applications.

Servlet is an API specification. That will be used by vendor (creators of s/w’s) companies as rules and guidelines to develop servlet container softwares and same thing will be used by programmers as base to develop java based web resource programs. Servlet API latest version is 3.0 but the running version is 2.5

When your web server (like Apache) gets a request for a servlet from the client, the server hands over the request not to the servlet itself, but to the servlet container in which servlet is deployed. The servlet container then directs the request to the appropriate servlet. The servlet does its processing which may include interacting with the database or other server side components such as servlets or JSPs (Java Server Pages). After the request is processed by servlet, the response (generally in the form of HTML Document) is returned back to the servlet container which in turn sends the response back to the client via the web server. It should be noted that a servlet is loaded by the servlet container, first time the servlet is requested. Afterwards, the servlet stays in memory waiting for other requests. Each request dispatched by the clients may be served simultaneously inside the same process and typically the servlet is unloaded only when the servlet container is shut down.

Servlet request response cycle

Servlets are generally used for

 

• Processing and / or storing data submitted using HTML form by the user such as purchase order or a credit card data.

• Providing dynamic contents (e.g. returning the results of a database query to the client).

• Allowing collaboration between people. A servlet can handle multiple requests concurrently i.e. they can synchronize requests to support systems such as online conferencing.

• Forwarding requests to other servers and servlets. This allows them to be used to balance load among several servers that mirror the same contents.

• Managing state information on the top of the stateless HTTP e.g. for an online shopping cart system which manages shopping carts from many concurrent customers and maps every request to the right customer.