Java Servlet Framework
In the HTTP based request-response paradigm, a client user agent (a web browser or any such application that
can make HTTP requests and receive HTTP responses) establishes a connection with a web server and sends a
request to the server. If the web server has a mapping to a servlet for the specified URL in the request, the
web server delegates the request to the specified servlet. The servlet in turn processes the request and
generates a HTTP response. For a description of the HTTP protocol and the request-response paradigm, see
For web application development, the servlet API provides three primary abstractions: HTTP requests, request
processing based on some application logic, and HTTP responses. These abstractions simplify the application
development as far as the requests and responses are concerned. In addition, the servlet API also provides a
mechanism for session tracking and state management. These are described below.
HTTP Request: The interface HttpServletRequest is the first abstraction provided by the
servlet API. This interface encapsulates HTTP request from a user agent. When a request is received by the
servlet engine, an object of this type is constructed and passed on to a servlet. This object provides methods
for accessing parameter names and values of the request, other attributes of the request, and an input stream
containing the body of the request.
HTTP Response: The HttpServletResponse interface of the servlet API provides an encapsulation
of the HTTP response generated by a servlet. This interface defines an object created by the servlet engine
that lets a servlet generate content in response to a user agent’s request. This object provides methods to
set type and length of the content, character encoding, cookies, response status including errors, and an
output stream into which binary response data may be written. Alternatively, this object also provides a print
writer object for writing formatted text responses.
Application Logic and Content Generation: The HttpServlet interface specifies methods for
implementing the application logic and generating content in response to a HTTP request. These methods handle
the GET, POST, HEAD, DELETE, OPTIONS, PUT and TRACE requests of HTTP. These methods are invoked by the servlet
engine and act as placeholders for implementing application logic. The servlet engine also supplies
HttpServletRequest and HttpServletResponse objects to these methods.
Figure 1: HTTP Servlet Framework
These interfaces are shown in Figure 1 in the context of a request and a response.
In this diagram, the service method of the HttpServlet is shown to implement the application logic
and content generation, although one of the doGet, doPost, doHead, doDelete,
doOptions, doPut or doTrace methods can handle HTTP requests.
Session Tracking and State Management: The HTTP, which is the underlying protocol for web applications,
is stateless. This protocol covers only a single request (with the connection initiated by the user agent) and
a response. In this protocol, irrespective of the status of the protocol, the connection may be closed by
either of the transacting parties. This has the following ramifications:
- The protocol has no mechanism by which a series of unique requests from a user agent may be identified as
being from the same user agent. Consequently, in a transaction spanning multiple requests and responses, the
web server can not uniquely determine that all the requests are from the same user agent. A user, therefore,
can not establish a dialog with the web server to perform a business transaction.
- Since connections are not maintained by either of the transacting parties, the state of the transaction
(and the application) can not be preserved across multiple requests.
The Java servlet API provides the HttpSession interface for session tracking and state management in
a session. A servlet obtains a HttpSession object from the associated HttpServletRequest
object. Servlets track sessions by URL rewriting or by cookies (See the tutorial on Servlet Essentials at
for more details). Objects of type HttpSession can be set or obtained for new and existing sessions
respectively from HttpServletRequest objects. Session specific data can be stored into these objects.