jump to navigation

The Request Object August 23, 2005

Posted by Coolguy in Servlets/Jsp.
Tags:
trackback
  • The request object encapsulates all information from the client request. In the HTTP protocol, this information is transmitted from the client to the server in the HTTP headers and the message body of the request.
  • 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
    • getParameterNames
    • getParameterValues
    • 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 ofS tring 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.
  • 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.
  • Attributes are objects associated with a request.
  • Attributes may be set by the container to express information that otherwise could not be expressed via the API, or may be set by a servlet to communicate information to another servlet (via theRequestDispatcher).
  • Attributes are accessed with the following methods of the ServletRequest interface:
    • getAttribute
    • getAttributeNames
    • setAttribute
  • Only one attribute value may be associated with an attribute name.
  • Attribute names beginning with the prefixes of “java.” and “javax.” are reserved.Similarly, attribute names beginning with the prefixes of “sun.”, and “com.sun.” are reserved for definition by Sun Microsystems.
  • A servlet can access the headers of an HTTP request through the following methodsof the HttpServletRequest interface:
    • getHeader
    • getHeaders
    • getHeaderNames
  • 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.
  • 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
    • getDateHeader
  • 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.
  • The request path that leads to a servlet servicing a request is composed of manyimportant sections.
  • Context Path: The path prefix associated with the ServletContext that this servlet is a part of.
  • Servlet Path: The path section that directly corresponds to the mapping which activated this request.
  • PathInfo: The part of the request path that is not part of the Context Path or the Servlet Path.
  • If the context is not rooted at the root of the server’s name space, the context path starts with a’/’ character but does not end with a’/’ character.
  • Servlet Path starts with a’/’ character except in the case where the request is matched with the ‘/*’ pattern, in which case it is an empty string.
  • PathInfo is either null if there is no extra path, or is a string with a leading ‘/’.
  • The following methods exist in the HttpServletRequest interface to access this information:
    • getContextPath
    • getServletPath
    • getPathInfo
  • requestURI = contextPath + servletPath + pathInfo
  • E.g: SetupContext Path /catalogServlet Mapping Pattern: /lawn/*Servlet: LawnServletServlet Mapping Pattern: /garden/*Servlet: GardenServletServlet Mapping Pattern: *.jspServlet: JSPServletRequest Path : /catalog/lawn/index.html ContextPath: /catalogServletPath: /lawnPathInfo: /index.htmlRequest Path :/catalog/garden/implements/ ContextPath: /catalogServletPath: /gardenPathInfo: /implements/Request Path /catalog/help/feedback.jsp ContextPath: /catalogServletPath: /help/feedback.jspPathInfo: null
  • There are two convenience methods in the API which allow the Developer to obtain the file system path equivalent to a particular path. These methods are:
    • ServletContext.getRealPath
    • HttpServletRequest.getPathTranslated
  • The getRealPath method takes a String argument and returns a String representation of a file on the local file system to which a path corresponds.
  • The getPathTranslated method computes the real path of the pathInfo of the request.
  • The HttpServletRequest interface provides the getCookies method to obtain an array of cookies that are present in the request.
  • Typically, the only information that the client sends back as part of a cookie is the cookie name and thecookie value. Other cookie attributes that can be set when the cookie is sent to the browser, such as comments, are not typically returned.
  • If a request has been transmitted over a secure protocol, such as HTTPS, this information must be exposed via the isSecure method of the ServletRequest interface.
  • If there is an SSL certificate associated with the request, it must be exposed by the servlet container to the servlet programmer as an array of objects of type java.security.cert.X509Certificate and accessible via a ServletRequest attribute of javax.servlet.request.X509Certificate.
  • Clients may optionally indicate to aWeb server what language they would prefer the response be given in. This information can be communicated from the client using the Accept-Language header along with other mechanisms described in the HTTP/1.1 specification.
  • The following methods are provided in the ServletRequest interface to determine the preferred locale of the sender:
    • getLocale
    • getLocales
  • The getLocale method will return the preferred locale for which the client wants to accept content.
  • The getLocales method will return an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client.
  • If no preferred locale is specified by the client, the locale returned by the getLocale method must be the default locale for the servlet container
  • The default encoding of a request the container uses to create the request reader and parse POST data must be “ISO-8859-1” if none has been specified by the client request.
  • If the client hasn’t set character encoding and the request data is encoded with a different encoding than the default as described above, breakage can occur. To remedy this situation, a new method setCharacterEncoding(String enc) has been added to the ServletRequest interface.
  • Developers can override the character encoding supplied by the container by calling this method. It must be called prior to parsing any post data or reading any input from the request. Calling this method once data has been read will not affect the encoding.
  • Each request object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method.
  • Containers commonly recycle request objects in order to avoid the performance overhead of request object creation.
  • getInputStream() could be used to get the raw bytes of everything that comes with a request
  • getRemotePort() gets the clients port
  • getServerPort() tells the port to which the request was originally sent
  • getLocalPort() tells on which port the request finally ended up.
  • Although all the requests are sent to a single port where the server is listening, server turns around and finds a different local port for each thread so that the app can handle multiple clients at the same time.
Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: