More JSP Implicit Objects
Session Object
The session implicit object is used to provide an association between the client
and the server. This association, or session, persists over multiple connections and/or requests
during a given time period. Sessions are used to maintain state and user identity across multiple
page requests.
A session can be maintained either by using cookies or by URL rewriting. To expose whether
the client supports cookies, session defines an isCookieSupportDetermined() method and
an isUsingCookies() method.
Servlet Class
HttpSession
The following table summarises the most useful methods available to the session
object. For more details consult the Servlet API.
| Method | Description |
|---|---|
isNew() |
A session is considered to be "new" if it has been created by the server, but the client has not yet acknowledged joining the session. |
invalidate() |
Discards the session, releasing any objects stored as attributes. |
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. |
Application Object
This implicit object represents the application to which the JSP belongs. JSPs are grouped into applications according to their URLs where the first directory name in a URL defines the application.
The application object contains methods that provide information about the JSP
Container, support for logging plus utility methods for example translating a relative URL to an
absolute path.
Servlet Class
javax.servlet.ServletContext
The following table summarises the most useful methods available to the session
object. For more details consult the Servlet API.
| Method | Description |
|---|---|
getAttribute(String objName) |
Returns the object named objName or returns null if the objName does not exist. |
getAttributeNames() |
Returns an Enumeration containing the attribute names available within the application. |
setAttribute(String objName, Object object) |
Stores the object with the given object name in this application. |
removeAttribute(String objName) |
Removes the objName object from the application. |
getMajorVersion() |
Returns the major version of the Servlet API for the JSP COntainer. |
getServerInfo() |
Returns information about the JSP Container, typically, name and product version. |
getResourceAsStream(Path) |
Translates the resource URL into an input stream for reading. |
log(Message) |
Writes a text string to the JSP Containers default log file. |
Using Application, Session and Request Attributes
The ability to share an object between JSP pages at either the request, session or application level can greatly enhance your JSP application. It is important to note that this object is shared between pages rather than copied and passed between pages. This point is highlighted in the second example.
Objects shared at the application level are accessible from all user essions. This means that
data such as user name or credit card details should never be held as an application attributes. A
good example of what can be held at the application level is a hit counter. The need to hold objects held at the
request level is rare except when working with <jsp:include/> and <jsp:forward/>
tags which we will cover later. Another point to note about attributes is that they can only hold
objects variables of primitive data types
can't be shared in this way.
The following example illustrates sharing an object as an application attribute. The first JSP creates a new Array object and sets it as an application attribute:
buildArray.jsp
<%
//Define and build array
String[] myArray = new String[4];
myArray[0] = "Hello ";
myArray[1] = "there! ";
myArray[2] = "How are ";
myArray[3] = "you ? ";
//Set application attribute
application.setAttribute("StoredArray", myArray);
%>
This second JSP gets the application attribute. Note this use of the cast when retreiving attributes:
fetchArray.jsp
<%
//Retrieve application attribute
String[] anotherArray =
(String[])applicaton.getAttribute("StoredArray");
//Write out array values
out.println(anotherArray[0]);
out.println(anotherArray[1]);
out.println(anotherArray[2]);
out.println(anotherArray[3]);
%>
The output from the second JSP is:
Hello there! How are you ?
The following demo makes use of the application object to share data between 2 JSPs.
Summary
This topic looked at the implicit objects that are available to a JavaServer Page. The
examples have shown that these methods are very useful when working with JSPs for interacting with
the client (request, response) and for sharing information between pages (session,
application).