In order to use the servlet pipeline, you should be familiar with the Servlet interface and the servlet model for handling requests. This section outlines basic concepts.

The role of the web server can be summarized as parsing HTTP requests into request/response object pairs, HttpServletRequest and HttpServletResponse, respectively. These object pairs are relayed to servlets that actually handle the requests. A servlet services each request by examining request parameters and producing the appropriate output.

Request Handling

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.

Generated Response

After the web server receives the request, it generates output to send back to the browser. The output includes a response code such as 404 or 200, header data, and the response data, which can consist of 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 response data is written directly through a javax.servlet.ServletOutputStream, which can be obtained from the HttpServletResponse.

Servlet Interface

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

Copyright © 1997, 2014 Oracle and/or its affiliates. All rights reserved. Legal Notices