In order to use the servlet pipeline, you should be familiar with the Servlet interface and the servlet model for handling requests. The complete specifications are available from JavaSoft, but the basic concepts are presented here.

When a Web server receives a request, it receives a stream of information from the browser. This information is parsed into different parts, such as a request URI, query arguments, headers, and cookies (a subset of the headers). This information is packaged into a single Java object called a javax.servlet.http.HttpServletRequest.

A request might also carry additional information depending on the type of the request. For example, a form submitted through a POST request uses this additional information to pass the form submission arguments. This additional information can be read as a stream from a javax.servlet.ServletInputStream, which can be obtained from the HttpServletRequest.

After the Web server receives the request, it must generate output to send back to the browser. The output is also formatted in a special way—a response code is sent such as 404 or 200, followed by headers, and the actual data of the response—an HTML page, an image, and so on. Methods for setting the response code and headers are encapsulated in a javax.servlet.http.HttpServletResponse. The actual response data is written directly through a javax.servlet.ServletOutputStream, which can be obtained from the HttpServletResponse.

Thus, the entire request/response cycle can be represented by an HttpServletRequest/ HttpServletResponse pair. A servlet is an object that can handle requests in the form of HttpServletRequest and HttpServletResponse objects, and service the request by examining the parameters of the request and producing the appropriate output. A servlet must implement the javax.servlet.Servlet interface. This interface defines the service method that is called to handle a request:

void service (ServletRequest, ServletResponse)
  throws ServletException, IOException

This means that the role of the Web server is reduced to parsing HTTP requests into request/response object pairs. These object pairs are then passed off to servlets that actually handle the requests.

 
loading table of contents...