In the previous chapter we saw about the various scopes for variables that can be used in a J2EE Web Application. In this chapter, we are going to understand the Request in detail.
So, Lets get started!!!
The Http Request
When a user hits a URL with a servlet at the other end, the Servlet Container creates an HttpServletRequest object. It passes this object as an argument to the servlet's service methods (doPut(), doGet(), and doPost()). There is a lot of information in this object, including the login details of the user making this request and the name of the HTTP method with which this request was made. However, for this exam objective is restricted to header information.
Therefore, you need to know the following HttpServletRequest methods.
The following list of methods are the ones from the classes ServletRequest and HttpServletRequest you need to know in order to pass the exam.
• getAttribute(String name) - Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
• getAttributeNames() - Returns an Enumeration containing the names of the attributes available to this request.
• getAuthType() - Returns the name of the authentication scheme used to protect the servlet.
• getCharacterEncoding() - Returns the name of the character encoding used in the body of this request.
• getContentLength() - Returns the length, in bytes, of the request body if made available by the input stream, or -1 if the length is not known.
• getContentType() - Returns the MIME type of the body of the request, or null if the type is not known.
• getContextPath() - Returns the portion of the request URI that indicates the context of the request.
• getCookies() - Returns an array containing all of the Cookie objects the client sent with this request.
• getDateHeader(java.lang.String name) - Returns the value of the specified request header as a long value that represents a Date object.
• getHeader(java.lang.String name) - Returns the value of the specified request header as a String.
• getHeaderNames() - Returns an enumeration of all the header names this request contains.
• getHeaders(java.lang.String name) - Returns all the values of the specified request header as an Enumeration of String objects.
• getInputStream() - Retrieves the body of the request as binary data using a ServletInputStream.
• getIntHeader(java.lang.String name) - Returns the value of the specified request header as an int.
• getLocale() - Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.
• getLocales() - Returns an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client based on the Accept-Language header.
• getMethod() - Returns the name of the HTTP method with which this request was made; for example, GET, POST, or PUT.
• getParameter(java.lang.String name) - Returns the value of a request parameter as a string, or null if the parameter does not exist.
• getParameterMap() - Returns a java.util.Map of the parameters of this request.
• getParameterNames() - Returns an Enumeration of string objects containing the names of the parameters contained in this request.
• getParameterValues(java.lang.String name) - Returns an array of string objects containing all of the values the given request parameter has, or null if the parameter does not exist.
• getPathInfo() - Returns any extra path information associated with the URL the client sent when it made this request.
• getPathTranslated() - Returns any extra path information after the servlet name but before the query string, and translates it to a real path.
• getProtocol() - Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1.
• getQueryString() - Returns the query string that is contained in the request URL after the path.
• getReader() - Retrieves the body of the request as character data using a BufferedReader.
• getRemoteAddr() - Returns the Internet Protocol (IP) address of the client that sent the request.
• getRemoteHost() - Returns the fully qualified name of the client that sent the request.
• getRemoteUser() - Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
• getRequestDispatcher(java.lang.String path) - Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
• getRequestURI() - Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
• getRequestURL() - Reconstructs the URL the client used to make the request.
• getRequestedSessionId() - Returns the session ID specified by the client.
• getScheme() - Returns the name of the scheme used to make this request; for example, http, https, or ftp.
• getServerName() - Returns the host name of the server that received the request.
• getServerPort() - Returns the port number on which this request was received.
• getServletPath() - Returns the part of this request's URL that calls the servlet.
• getSession() - Returns the current session (HttpSession) associated with this request, or if the request does not have a session, creates one.
• getSession(boolean create) - Returns the current HttpSession associated with this request, or, if there is no current session and create is true, returns a new session.
• getUserPrincipal() - Returns a java.security.Principal object containing the name of the current authenticated user.
• isRequestedSessionIdFromCookie() - Checks whether the requested session ID came in as a cookie.
• isRequestedSessionIdFromURL() - Checks whether the requested session ID came in as part of the request URL.
• isRequestedSessionIdValid() - Checks whether the requested session ID is still valid.
• isSecure() - Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
• isUserInRole(java.lang.String role) - Returns a boolean indicating whether the authenticated user is included in the specified logical “role”.
• removeAttribute(java.lang.String name) - Removes an attribute from this request.
• setAttribute(java.lang.String name, java.lang.Object o) - Stores an attribute in this request.
• setCharacterEncoding(java.lang.String env) - Overrides the name of the character encoding used in the body of this request.
Several of the HttpServletRequest methods are not mentioned specifically in the objectives, but you should be familiar with them.
Previous Chapter: Chapter 13 - Web Application Scope
Next Chapter: Chapter 15 - The Session
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.