Servlet Request

The first part of a request will be the HTTP headers, followed by the parameters. Headers are the bookkeeping information supplied automatically by the browser or server, stating things like the locale, and the version of HTTP in use. Parameters are provided by the user and passed in the query string or in the form data. A parameter name is whatever name the HTML form designer gave the parameter in an attribute. It is legal for parameter or headers to have a comma-separated list of values, so you need to be alert to this possibility in your code.

Let's take a look at the classes that implement request and response objects to see what information comes and goes. We'll start with the Java class that represents an HTTP request. Tomcat will create an object that implements this interface to hold all the data in the incoming request. Tomcat will then invoke your servlet, and pass the request object to it as an argument.

These methods are just the highlights of an HTTP servlet request object. There are about 20 getSomething() methods in HttpServletRequest, and another 20 in its parent, ServletRequest, allowing all information in the request to be retrieved. You will invoke these methods (shown in Table 26-5) on the javax.servlet.http.HttpServletRequest parameter, as shown in the example coming up.

Table 26-5. Key Methods of javax.servlet.http.HttpServletResponse

Method

Purpose

getWriter()

Returns a PrintWriter that will get written with the data part of the servlet response.

setHeader(String n, String v)

Adds a response header with the given name and value.

setDateHeader(String s, long d)

Adds a response header with the given name and time value.

setIntHeader(String s, int v)

Adds a response header with the given name and int value.

addCookie(Cookie c)

Adds the specified cookie to the response.

setStatus(int sc)

Set the status code for this response.

setContentType (String s)

Sets the response's MIME content type.

setContentLength (int size)

Sets the Content-Length header of the response.

Notice that some of these methods, such as getHeaders(), return Enumeration objects, rather than the newer Iterator object that was intended to replace Enumeration and was part of JDK 1.2. This is for backwards compatibility with existing servlet code.

Table 26-4. Key methods of javax.servlet.http.HttpServletRequest

Method

Purpose

getHeader(String s)

Returns the first value of the header whose name you provide, or null if there isn't one of that name.

getHeaderNames()

Returns an Enumeration of all the header names in this request.

getHeaders (String s)

Returns an Enumeration of all the values of the header whose name you provide, or an empty Enumeration if there isn't one of that name. Some headers, such as Accept-Language, can be sent by clients as several headers each with a different value rather than sending the header as a comma-separated list.

getIntHeader (String s)

Used when you want to pull an integer out of the headers. String s is the name of the header. It will return the int value of the header, or throw a NumberFormatException, or return -1 if there is no header with this name.

getParameter (String s)

Returns the value of the parameter whose name you provide, or null if there isn't one.

getParameterValues(String s)

Works like getParameter(), but is used when the parameter can have several values, e.g., it may be a set of checkboxes or a multiselection list. It returns an array of String containing all the values.

getParameterNames()

Returns a java.util.Enumeration object containing all the parameter names in this request.

getServerName()

Returns the hostname of the server that received the request.

getCookies()

Returns an array of the cookies that came with this request. A cookie is a few bytes of data that the server sends to the browser and gets back in later requests, allowing the server to keep track of the client.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.142.194