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:

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:

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:

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();
	...
}
					

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide


Hosting provided by PerfoHost: KVM VPS provider. See PefoHost's KVM VPS vs OpenVZ/Virtuozzo vs XEN VPS comparative chart.
CRM-exporter: Vehicle Database Script 1999-2011