Previous | Next | Trail Map | Servlets | Interacting with Clients

Requests and Responses

Methods in the HttpServlet class that handle client requests take two arguments:

  1. An HttpServletRequest object, which encapsulates the data from the client
     
  2. An HttpServletResponse object, which encapsulates the response to the client
     

HttpServletRequest Objects

An HttpServletRequest object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. The HttpServletRequest object also allows you to obtain the arguments that the client sent as part of the request.

To access client data:
 


Note: Use either a getParameter[Values] method or one of the methods that allow you to parse the data yourself. They can not be used together in a single request.

 

HttpServletResponse Objects

An HttpServletResponse object provides two ways of returning data to the user:
 

Use the getWriter method to return text data to the user, and the getOutputStream method for binary data.

Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete.
 

HTTP Header Data

You must set HTTP header data before you access the Writer or OutputStream. The HttpServletResponse class provides methods to access the header data. For example, the setContentType method sets the content type. (This header is often the only one manually set.)


Previous | Next | Trail Map | Servlets | Interacting with Clients