|
|
Interacting with Clients |
Methods in the
HttpServletclass that handle client requests take two arguments:
- An
HttpServletRequestobject, which encapsulates the data from the client
- An
HttpServletResponseobject, which encapsulates the response to the client
HttpServletRequest Objects
An
HttpServletRequestobject provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. TheHttpServletRequestobject also allows you to obtain the arguments that the client sent as part of the request.To access client data:
- The
getParametermethod returns the value of a named parameter. If your parameter could have more than one value, usegetParameterValuesinstead. ThegetParameterValuesmethod returns an array of values for the named parameter. (The methodgetParameterNamesprovides the names of the parameters.)
- For HTTP GET requests, the
getQueryStringmethod returns aStringof raw data from the client. You must parse this data yourself to obtain the parameters and values.
- For HTTP POST, PUT, and DELETE requests,
- If you expect text data, the
getReadermethod returns aBufferedReaderfor you to use to read the raw data.
- If you expect binary data, the
getInputStreammethod returns aServletInputStreamfor you to use to read the raw data
Note: Useeither agetParameter[Values]methodor 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
HttpServletResponseobject provides two ways of returning data to the user:
- The
getWritermethod returns aWriter
- The
getOutputStreammethod returns aServletOutputStream
Use the
getWritermethod to return text data to the user, and thegetOutputStreammethod for binary data.Closing the
WriterorServletOutputStreamafter 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
WriterorOutputStream. TheHttpServletResponseclass provides methods to access the header data. For example, thesetContentTypemethod sets the content type. (This header is often the only one manually set.)
|
|
Interacting with Clients |