Tutorial Home
Hibernate
StrutsJSP
Spring
|
SCWCD : HttpServletRequest Interface
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:
Headers. A servlet can access the headers of an HTTP request through the following methods of the HttpServletRequest interface:
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:
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 6.0 Simulator 664+ Questions With Explanations
SCWCD 5.0 Simulator 556+ Questions With Explanations
SCWCD 4.0 Simulator 500+ Questions With Explanations
OCA 10g Simulator 594+ Questions With Explanations
Hiring made easy Harness social networking, employee referrals, Agency portal and Job Portals
