805 OCPJP 6 Questions 788 SCJP 5 questions 650 OCPJWCD 5 questions 600 OCAJP 7 questions 610 OCPJP 7 questions 510 Upgrade to OCPJP 7 questions 900 Java J2EE Qns

Tutorials

SCWCD : HttpServletRequest Interface

Using the HttpServletRequest interface, write code to retrieve HTML form parameters from the request, retrieve HTTP request header information, or retrieve cookies from the request.

HTTP Protocol Parameters.

Request parameters for the servlet are the strings sent by the client to a servlet container as part of its request. When the request is an HttpServletRequest object, the container populates the parameters from the URI query string and POST-ed data.

The parameters are stored as a set of name-value pairs. Multiple parameter values CAN exist for any given parameter name. The following methods of the ServletRequest interface are available to access parameters:

  • getParameter

    Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

    You should only use this method when you are sure the parameter has only ONE value. If the parameter might have MORE than one value, use getParameterValues(String).

    If you use this method with a multivalued parameter, the value returned is equal to the FIRST value in the array returned by getParameterValues.

    If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.

  • getParameterNames

    Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an EMPTY Enumeration.

  • getParameterValues

    Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. If the parameter has a single value, the array has a length of 1.

  • getParameterMap

    Returns an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.

public interface ServletRequest {

	public java.lang.String getParameter(java.lang.String name);
	public java.util.Enumeration getParameterNames();
	public java.lang.String[] getParameterValues(java.lang.String name);
	public java.util.Map getParameterMap();

}
					

The getParameterValues method returns an array of String objects containing all the parameter values associated with a parameter name. The value returned from the getParameter method must be the FIRST value in the array of String objects returned by getParameterValues. The getParameterMap method returns a java.util.Map of the parameter of the request, which contains names as keys and parameter values as map values.

public void doPost(HttpServletRequest request, HttpServletResponse res)
		throws IOException, ServletException {
	Enumeration e = request.getParameterNames();
	PrintWriter out = res.getWriter ();
	while (e.hasMoreElements()) {
		String name = (String)e.nextElement();
		String value = request.getParameter(name);
		out.println(name + " = " + value);
	}
}					

Data from the query string and the post body are aggregated into the request parameter set. Query string data is presented BEFORE post body data. For example, if a request is made with a query string of a=hello and a post body of a=goodbye&a=world, the resulting parameter set would be ordered a=(hello, goodbye, world).

The following are the conditions that must be met before post FORM data will be populated to the parameter set:

  1. The request is an HTTP or HTTPS request.

  2. The HTTP method is POST.

  3. The content type is application/x-www-form-urlencoded.

  4. The servlet has made an initial call of any of the 'getParameter' family of methods on the request object.

If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object's input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object's input stream.

Headers.

A servlet can access the headers of an HTTP request through the following methods of the HttpServletRequest interface:

  • getHeader

    Returns the value of the specified request header as a String. If the request did not include a header of the specified name, this method returns null. If there are multiple headers with the same name, this method returns the first head in the request. The header name is case insensitive. You can use this method with any request header.

  • getHeaders

    Returns all the values of the specified request header as an Enumeration of String objects.

    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. If the request did not include any headers of the specified name, this method returns an EMPTY Enumeration. The header name is case INSENSITIVE. You can use this method with any request header.

  • getHeaderNames

    Returns an enumeration of all the header names this request contains. If the request has no headers, this method returns an empty enumeration.

The getHeader method returns a header given the name of the header. There can be multiple headers with the same name, e.g. Cache-Control headers, in an HTTP request. If there are multiple headers with the same name, the getHeader method returns the first header in the request. The getHeaders method allows access to all the header values associated with a particular header name, returning an Enumeration of String objects.
public interface HttpServletRequest {

	public java.lang.String getHeader(java.lang.String name);
	public java.util.Enumeration getHeaders(java.lang.String name);
	public java.util.Enumeration getHeaderNames();

}
					

Headers may contain String representations of int or Date data. The following convenience methods of the HttpServletRequest interface provide access to header data in a one of these formats:

  • getIntHeader

    Returns the value of the specified request header as an int. If the request does not have a header of the specified name, this method returns -1. If the header cannot be converted to an integer, this method throws a NumberFormatException.

    The header name is case INSENSITIVE.

  • getDateHeader

    Returns the value of the specified request header as a long value that represents a Date object. Use this method with headers that contain dates, such as If-Modified-Since.

    The date is returned as the number of milliseconds since January 1, 1970 GMT. The header name is case insensitive.

    If the request did not have a header of the specified name, this method returns -1. If the header can't be converted to a date, the method throws an IllegalArgumentException.

If the getIntHeader method cannot translate the header value to an int, a NumberFormatException is thrown. If the getDateHeader method cannot translate the header to a Date object, an IllegalArgumentException is thrown.
public interface HttpServletRequest {

	public int getIntHeader(java.lang.String name);
	public long getDateHeader(java.lang.String name);

}
					

public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	Enumeration e = request.getHeaderNames();
	while (e.hasMoreElements()) {
		String name = (String)e.nextElement();
		String value = request.getHeader(name);
		out.println(name + " = " + value);
	}
}
					

Cookies.

The HttpServletRequest interface provides the getCookies method to obtain an array of cookies that are present in the request. This method returns null if no cookies were sent.

The cookies are data sent from the client to the server on every request that the client makes. Typically, the only information that the client sends back as part of a cookie is the cookie name and the cookie value. Other cookie attributes that can be set when the cookie is sent to the browser, such as comments, are not typically returned. Several cookies might have the same name but different path attributes.

public interface HttpServletRequest {

	public Cookie[] getCookies();

}
					
package javax.servlet.http;

public class Cookie implements java.lang.Cloneable {
	...
	public Cookie(java.lang.String name, java.lang.String value);
	public java.lang.String getName();
	public java.lang.String getPath();
	public java.lang.String getValue();
	...
}
					

scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications
www.javacertifications.net