Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.
These objects will be automatically instantiated under specific variable names hence the name implicit. Furthermore, as summarized below, each object must adhere to a specific Java class or interface definition.
We have already met one of these objects already - the out
object in which we used the println() method to add text to the
output stream.
| Object | Class or Interface | Description |
|---|---|---|
page |
jsp.HttpJspPage |
Page's servlet instance |
config |
ServletConfig |
Servlet configuration information |
pageContext |
jsp.pageContext |
Provides access to all the namespaces associated with a JSP page and access to several page attributes |
request |
http.HttpServletRequest |
Data included with the HTTP Request |
response |
http.HttpServletResponse |
HTTP Response data, e.g. cookies |
out |
jsp.JspWriter |
Output stream for page context |
session |
http.HttpSession |
User specific session data |
application |
ServletContext |
Data shared by all application pages |
Of these the first three are rarely used. The application,
session and request implicit objects have the
additional ability to hold arbitrary values. By setting and getting attribute
values these objects are able to share information between several JSP pages.
This will be discussed later.
The request object retrieves the values that the client browser
passed to the server during an HTTP request such as headers, cookies or parameters
associated with the request. Among the most common use of the request
object is to obtain parammeter or query string values. The following demo
illustrates how to use the request object for parsing form data.
javax.servlet.HttpServletRequest
The following table summarises the most useful methods available to the
request object. For more details consult the Servlet
API.
| Method | Description |
|---|---|
getParameter(String name) |
Returns the value of a request parameter as a String, or null if the parameter does not exist. |
getParameterNames() |
Returns an Enumeration of String objects containing the names of the parameters contained in this request. |
getParameterValues(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. |
getCookies() |
Gets the array of cookies found in this request. See below for more details on working with cookies. |
getQueryString() |
Gets any query string that is part of the HTTP request URI. |
getRequestURI() |
Gets the URI to the current JSP page. |
getHeaderNames() |
Returns an enumerator of all HTTP header names. |
getHeader(String hdr) |
Returns the value of an HTTP header such as "QUERY_STRING" or "URL".
The method getHeaderNames() can be used to determine what
headers are available. See example below. |
getAttribute(String) |
Retrives the object associated with the named attribute. |
getAttributeNames() |
Retrives the names of all attributes currently associated with the session. |
setAttribute(String, object) |
Sets the object to the named attribute. attribute created if it doesn't exist. |
removeAttribute(String) |
Removes the object bound with the specified name from this session. |
The getCookies() method of the request object
returns an array of Cookie objects. Cookies are used to allow
web browsers to hold small amounts of information or state data associated
with a user's web browsing. Common applications for cookies include storing
user preferences, automating low security user "sign on" facilities, and
helping collect data used for "shopping cart" style applications.
Cookies are named and have a single value. They may have optional attributes, including a comment presented to the user, path and domain qualifiers for which hosts see the cookie, a maximum age, and a version.
The following constructs a cookie with a specified name and value.
Cookie(java.lang.String name, java.lang.String value)
See the following section on the response object on how to create cookies.
Cookie objects have the following methods.
| Method | Description |
|---|---|
getComment() |
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined. |
getMaxAge() |
Returns the maximum specified age of the cookie. |
getName() |
Returns the name of the cookie. |
getPath() |
Returns the prefix of all URLs for which this cookie is targetted. |
getValue() |
Returns the value of the cookie. |
setComment(String) |
If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment. |
setMaxAge(int) |
Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behaviour: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted |
setPath(String) |
This cookie should be presented only with requests beginning with this URL. |
setValue(String) |
Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers. |
The following example obtains a list of available cookies.
Cookie[] myCookies = request.getCookies();
for(int n=0; n < myCookies.length; n++)
{
out.print(myCookies[n].getName() + " ");
out.print(myCookies[n].getValue() + "<BR>");
}
This example generates a table of HTTP headers and their respective values.
<table border=1 cellspacing=0 cellpadding=2>
<%
String strHeaderName = "";
for(Enumeration e = request.getHeaderNames();
e.hasMoreElements() ;)
{
strHeaderName = (String)e.nextElement();
%>
<tr>
<td><%= strHeaderName %></td>
<td><%= request.getHeader(strHeaderName)%> </td>
</tr>
<%
}
%>
</table>
The following workshop looks at using the request and
response objects.