Servlet Development Guide : PART I Introduction : Chapter 2 The Servlet Container and Servlet Concepts : Servlets
Servlets
Each servlet focuses only on the specific content that it creates. Because servlets contain mostly application logic, there is no single way to summarize servlets. This section presents a general overview of the interaction between a servlet and the servlet container. For details on servlet programming, see the focused topics and example programs in Part III . For instructions on deploying and configuring servlets, see Chapter 4, “Configuring Objects in the Servlet Container.”
Request and Response
Although the basic concepts behind servlets are protocol-independent, servlets are optimized to work with request/response protocols, such as HTTP. HTTP is a simple protocol. A client sends a request to a server and the server sends a response back to the client. The HTTP protocol is stateless, that is, the protocol treats each request and response independently. The protocol does not associate a given request with previous requests from the client or responses from the server.
The servlet container represents these concepts directly. Each time a client sends a request to a servlet, the container provides the servlet with a request object and a response object. The request object encapsulates the request from the client. The response object provides a suite of functions for creating a response.
Maintaining State
Although HTTP itself is stateless, the servlet container uses a simple strategy to manage state between requests. A servlet can store arbitrary data in a container-provided object called a session. The servlet container associates a unique ID with the session and returns the ID to the client. When another request arrives from the same client, the servlet container uses the unique ID to attach the session to the request. A servlet can maintain state with little effort, since the servlet container handles the mechanics.