The Request Object in JSP

By: Sathya Narayana Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The request object is an instance of a class that implements the javax.servlet.http.HttpServletRequest interface. It represents the request made by the client and makes the following methods available.

getAttribute() returns the value of the specified request attribute name. The return value is an Object or subclass if the attribute is available to the invoking ServletRequest object or null if the attribute isn't available:

public Object getAttribute(String name)

getAttributeNames() returns an Enumeration containing the attribute names available to the invoking ServletRequest object:

public java.util.Enumeration getAttributeNames()

getAuthType() returns the name of the authentication scheme used in the request or null if no authentication scheme was used. It returns one of the constants BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, or DIGEST_AUTH, or it returns null if the request wasn't authenticated:

public String getAuthType()

getCharacterEncoding() returns a String object containing the character encoding used in the body of the request or null if there's no encoding:

public String getCharacterEncoding()

getContentLength() returns the length of the body of the request in bytes or -1 if the length isn't known:

public int getContentLength()

getContentType() returns a String object containing the Multipurpose Internet Mail Extensions (MIME) type ("text/plain", "text/html", "image/gif", etc.) of the body of the request or null if the type isn't known:

public String getContentType()

getContextPath() returns the part of the request uniform resource indicator (URI) that indicates the context path of the request. The context path is the first part of the URI and always begins with the / character. For servlets running in the root context, this method returns an empty String. For example, if there's an incoming request from request http://localhost/guide/suburbs/index.jsp, then getContextPath()would return "/guide":

public String getContextPath()

getCookies() returns an array containing any Cookie objects sent with the request or null if no cookies were sent:

public Cookie[] getCookies()

getDateHeader() returns a long value that represents the date specified in the named header and is the number of milliseconds since January 1, 1970, Greenwich mean time (GMT). This method is used with a header that contains a date, and it returns -1 if the request doesn't contain the specified header:

public long getDateHeader(String name)

getHeader() returns the value of the specified header expressed as a String object or null if the request doesn't contain the specified header:

public String getHeader(String name)

The following is an example Hypertext Transfer Protocol (HTTP) request:

GET /search?index=servlets+jsp HTTP/1.1
Accept: image/gif, image/jpg, */*
Accept-Encoding: gzip
Connection: Keep-Alive
Cookie: userID=id66589
Host: www.mycompany.com
Referer: http://www.mycompany.com/getproducts.html
User-Agent: Mozilla/4.6 [en] (WinXP; U)

For example, if the usage is getRequest("Connection"), it'd return "Keep-Alive".

getHeaderNames() returns an Enumeration containing all of the header names used by the request:

public java.util.Enumeration getHeaderNames()

getHeaders() returns an Enumeration containing all of the values associated with the specified header name. The method returns an empty enumeration if the request doesn't contain the specified header:

public java.util.Enumeration getHeaders(String name)

getInputStream() returns a ServletInputStream object that can be used to read the body of the request as binary data:

public ServletInputStream getInputStream()
throws java.io.IOException

getIntHeader() returns the value of the specified header as an int. It returns -1 if the request doesn't contain the specified header, and it throws a NumberFormatException if the header value can't be converted to an int. This method was made for convenience when the header type is known to be an integer; this way it can be absorbed by the code without any conversion:

public int getIntHeader(String name)

getLocalAddr() returns the Internet Protocol (IP) address of the server where the request was received:

public String getLocalAddr()

getLocale() returns the preferred locale of the client that made the request:

public java.util.Locale getLocale()

getLocales() returns an Enumeration containing, in descending order of preference, the locales that are acceptable to the client machine:

public java.util.Enumeration getLocales()

getLocalName() returns the hostname of the server where the request was received:

public String getLocalName()

getLocalPort() returns the port on the server where the request was received:

public int getLocalPort()

getMethod() returns the name of the HTTP method used to make the request. Typical return values are "GET", "POST", or "PUT":

public String getMethod()

getParameter() returns a String object containing the value of the specified parameter or null if the parameter doesn't exist:

public String getParameter (String name )


getParameterMap() returns a Map containing the request parameters:

public java.util.Map getParameterMap()

getParameterNames() returns a Enumeration containing the parameters contained within the invoking ServletRequest object:

public java.util.Enumeration getParameterNames()

getParameterValues() is used when a parameter may have more than one value associated with it. The method returns a String array containing the values of the specified parameter or null if the parameter doesn't exist:

public String[] getParameterValues(String name)

getPathInfo() returns any additional path information contained in the request uniform resource locator (URL). This extra information will be after the servlet path and before the query string. It returns null if there's no additional path information. For example, in the incoming request from http://localhost/innersuburbs/, getPathInfo() would return "/innersuburbs":

public String getPathInfo()

getPathTranslated() returns the same information as the getPathInfo() method, but translated into a real path:

public String getPathTranslated()

getProtocol() returns the name and version of the protocol used by the request. A typical return String would be "HTTP/1.1":

public String getProtocol()

getQueryString() returns the query string that was contained in the request URL without any decoding from the container or null if there was no query string:

public String getQueryString()

getReader() returns a BufferedReader object that can be used to read the body of the request as character data:

public java.io.BufferedReader getReader()
throws java.io.IOException


getRemoteAddr() returns a String object containing the IP address of the client machine that made the request:

public String getRemoteAddr()

getRemoteHost() returns a String object containing the name of the client machine or the IP address if the name can't be determined:

public String getRemoteHost()

getRemotePort() returns the port number of the client or last proxy that sent the request:

public int getRemotePort()

getRemoteUser() returns the login of the user making the request or null if the user hasn't been authenticated:

public String getRemoteUser()

getRequestDispatcher() returns a RequestDispatcher object that acts as a wrapper around the resource located at the specified path. The path must begin with / and can be a relative path:

public RequestDispatcher getRequestDispatcher(String path)

getRequestedSessionId() returns the session ID that was specified by the client or null if the request didn't specify an ID:

public String getRequestedSessionId()

getRequestURI() returns a subsection of the request URL, from the protocol name to the start of the query string (the query string isn't included):

public String getRequestURI()

getRequestURL() reconstructs the URL used to make the request including the protocol, server name, port number, and path, but excluding the query string:

public StringBuffer getRequestURL()

getScheme() returns the scheme ("http", "https", "ftp", and so on) used to make the request:

public String getScheme()


getServerName() returns a String object containing the name of the server that received the request:

public String getServerName()

getServerPort() returns the port number that received the request:

public int getServerPort()

getServletPath() returns the part of the request URL that was used to call the servlet, without any additional information or the query string:

public String getServletPath()

getSession() returns the HttpSession object associated with the request. By default, if the request doesn't currently have a session, calling this method will create one. Setting the boolean parameter create to false overrides this:

public HttpSession getSession(boolean create)
public HttpSession getSession()

getUserPrincipal() returns a java.security.Principal object containing the name of the current authenticated user. If the user hasn't been authenticated, the method returns null:

public java.security.Principal getUserPrincipal()

isRequestedSessionIdFromCookie() returns true if the session ID came in from a cookie:

public boolean isRequestedSessionIdFromCookie()

isRequestedSessionIdFromURL() returns true if the session ID came in as part of the request URL:

public boolean isRequestedSessionIdFromURL()

isRequestedSessionIdValid() returns true if the session ID requested by the client is still valid:

public boolean isRequestedSessionIdValid()

isSecure() returns true if the request was made using a secure channel, for example, HTTPS:

public boolean isSecure ()


isUserInRole() returns true if the authenticated user has the specified logical role or false if the user isn't authenticated:

public boolean isUserInRole(String role)

removeAttribute() makes the specified attribute unavailable to the invoking ServletRequest object. Subsequent calls to the getAttribute() method for this attribute will return null:

public void removeAttribute(String name)

setAttribute() binds a value to a specified attribute name. Note that attributes will be reset after the request is handled:

public void setAttribute(String name,
Object o)

setCharacterEncoding() overrides the character encoding used in the body of this request:

public void setCharacterEncoding(String env)
throws java.io.UnsupportedEncodingException

The following String constants are used to identify the different types of authentication that may have been used to protect the servlet. They have the values BASIC, FORM, CLIENT_CERT, and DIGEST, respectively:

public static final String BASIC_AUTH
public static final String FORM_AUTH
public static final String CLIENT_CERT_AUTH
public static final String DIGEST_AUTH

These methods are deprecated and shouldn't be used in new code—they exist for compatibility with existing code:

public String getRealPath(String path)
public boolean isRequestedSessionIdFromUrl()

Use ServletContext.getRealPath(java.lang.String) instead of getRealPath(String path), and use ServletContext.isRequestedSessionIdFromURL() instead of isRequestedSessionIdFromUrl().



JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sathya Narayana
Querying Data with the JSTL in JSP
Deleting Data in MySQL
Modifying data and using WHERE clause in MySQL
Querying the Database in MySQL
Inserting Data into Tables in MySQL
Changing the Structure of an Existing Table in MySQL
The Exception Object in JSP
The Application Object in JSP
The Session Object in JSP
The Out Object in JSP
The Response Object in JSP
The Request Object in JSP
Tag Libraries in JSP
Scriptlets and Expressions in JSP
Declarations in JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Automatically Refreshing a JSP
JSP CheckBox Example
JSP Alert Example
JSP Program for display Date
What are the different scopes in JSP?
NotifyUtil::java.net.ConnectException: Connection refused: connect
Comparison operators in JSP
Sending Email using JSP
The Response Object in JSP
Embedding java codes in jsp sciptlets
JSP pages in servlet
Example Using Initialization Parameters
Simple HTML-Building Utilities
Most Emailed Articles (in JSP)
Scriptlets
Combining Scriptlets with HTML
Form processing in JSP
Protecting your website with a login page
NetworkClient
The Servlet Life Cycle
Closing Windows
WebClient: Talking to Web Servers Interactively
Drawbacks of the JSP Model 1 Architecture
Select Object
Working with Status Bar Messages
NotifyUtil::java.net.ConnectException: Connection refused: connect
What are the different scopes in JSP?
Calling JSP from a batch file
Embedding a QuickTime movie in a JSP