For most users, the typical web interaction can be represented as follows: Joe User enters a Uniform Resource Locator (URL) in the browser’s location box to request the resource that is identified by that URL. A URL is composed of a host name, an optional port (which defaults to 80), and the resource being requested.
When a web server receives that request, it first determines whether the resource being requested is static or dynamic in nature. A static resource is one that is identical no matter how many times, or in what order, that resource is requested. Some examples of these would be a company’s logo on a web page, a CSS style sheet, or a typical HTML page. The web server handles the request for a static resource by simply fetching the identified resource from the appropriate path in which its static content resides (for example, htdocs for Apache), and returning those bytes in the response to the user. For static resources, a depiction of this request flow is as shown below.
A dynamic resource, on the other hand, indicates a resource that varies its content based on various factors that may include the specific user making the request, the time of day when the request is made, or the presence of additional information in the request that affects how the request is interpreted. Some examples of these would include the current inventory for a specific product in an online store, or the availability of a seat on an airplane. Most web servers are tuned to deliver blazing fast request handling for static resources. However, they require additional help when dealing with dynamic resources. This help is typically provided by an additional software component that is registered with the web server, called a servlet container on the Java EE platform.
In the IIS world, access to this servlet container is offered by implementing an Internet Server Application Programming Interface (ISAPI) extension, which is a Dynamic Link Library (DLL) that allows you to extend the functionality of IIS. In the Apache world, you access the container using code written in C or C++. When a request comes in that is directed at a dynamic resource, the web server simply hands off that request to its associated servlet container. The container then springs into action, invokes the appropriate servlet that represents the requested dynamic resource. This servlet is responsible for generating the response.
The generated response is returned by the servlet container to the web server which, in turn, returns it to the requesting user. This interaction is depicted in the following diagram.
A servlet in its most basic form is a Java class that acts as a dynamic web resource. In other words, it can be the target of a client browser’s request. Being a Java class, it can tailor its response according to the payload of the incoming request, the conversation that this request is a part of, as well as other environmental criteria (such as the time of the day or the inventory status from a database).