The SESSION JSP tag library provides tags for reading or modifying client HttpSession information.
A servlet container uses an HttpSession to store information about a clients session on the server. By default a JSP page will create a session for a user. The user is tied to the session using either a Cookie or by using URL rewriting. This is how you can tie information to a user between multiple HTTP requests to your server.
Session Attributes are what makes it possible to store information about a clients session between multiple HTTP requests. A session attribute consists of a name and value. To save information about a users session on your server use the setAttribute tag.
The session attributes you save only exist while the sessions exists. Servlet containers will expire a users session and remove it after the user has been inactive for a period of time. Many servlet containers default to 30 minutes. You can change this by using the maxInactiveInterval tag.
A typical example of this is a shopping cart application where you use the session to track what items the user wants to purchase. Each time a user selects an item to purchase you set a session attribute which contains information about the item. Then when the user later checks out, you can determine what items they want to purchase by looking at the session attributes.
This custom tag library requires no software other than a servlet container that supports the JavaServer Pages Specification, version 1.1 or higher.
Follow these steps to configure your web application with this tag library:
<taglib> <taglib-uri>http://jakarta.apache.org/taglibs/session-1.0</taglib-uri> <taglib-location>/WEB-INF/session.tld</taglib-location> </taglib>
To use the tags from this library in your JSP pages, add the following directive at the top of each page:
<%@ taglib uri="http://jakarta.apache.org/taglibs/session-1.0" prefix="sess" %>
where "sess" is the tag name prefix you wish to use for tags from this library. You can change this value to any prefix you like.
session | Access general information about session. |
isNew | Determine if a session is new. |
invalidate | Invalidate a user session and remove it. |
maxInactiveInterval | Set the maximum inactive interval before a session times out. |
attribute | Get the value of a single session attribute. |
attributes | Loop through all session attributes. |
equalsAttribute | See if a session attribute equals some value. |
existsAttribute | See if a session attribute exists. |
removeAttribute | Removes an attribute from a session. |
setAttribute | Sets the value of a session attribute. |
session | Availability: 1.0 | ||||
Access general information about session. |
|||||
Tag Body | empty | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
id | Yes | No | 1.0 | ||
Script variable id for use with standard jsp:getProperty tag. |
|||||
Variables | Name | Scope | Availability | ||
id attribute value | Start of tag to end of page | 1.0 | |||
Session Data Bean |
|||||
Properties | Name | Get | Set | Availability | |
creationTime | Yes | No | 1.0 | ||
Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. |
|||||
sessionId | Yes | No | 1.0 | ||
Returns the unique identifier assigned to this session. |
|||||
lastAccessedTime | Yes | No | 1.0 | ||
Returns the last time the client sent a request associated with this session as the number of milliseconds since midnight January 1, 1970 GMT. |
|||||
maxInactiveInterval | Yes | No | 1.0 | ||
Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. |
|||||
Examples | Display the creation time of the session | ||||
|
isNew | Availability: 1.0 | ||||
Determine if a session is new. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
value | No | No | 1.0 | ||
If value is set to false the isNew tag includes body of tag if session is not new. Default value is true. |
|||||
Variables | None | ||||
Examples | Display whether the clients session is new | ||||
|
invalidate | Availability: 1.0 | ||||
Invalidate a user session and remove it. |
|||||
Tag Body | empty | ||||
Restrictions |
None |
||||
Attributes | None | ||||
Variables | None | ||||
Examples | Invalidate the users session and remove it | ||||
|
maxInactiveInterval | Availability: 1.0 | ||||
Set the maximum inactive interval before a session times out in seconds using the content of the tag body. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | None | ||||
Variables | None | ||||
Examples | Sets the session maximum inactive interval to 900 seconds | ||||
|
attribute | Availability: 1.0 | ||||
Get the value of a single session attribute. |
|||||
Tag Body | empty | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
name | Yes | No | 1.0 | ||
Name of session attribute to get. |
|||||
Variables | None | ||||
Examples | Output the value of the session attribute with name "test1" | ||||
|
attributes | Availability: 1.0 | ||||
Loop through all session attributes. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
id | Yes | No | 1.0 | ||
Script variable id for use with standard jsp:getProperty tag. |
|||||
Variables | Name | Scope | Availability | ||
id attribute value | Nested within tag | 1.0 | |||
Session Attribute Bean |
|||||
Properties | Name | Get | Set | Availability | |
name | Yes | No | 1.0 | ||
Session attribute name. |
|||||
value | Yes | No | 1.0 | ||
Session attribute value. |
|||||
Examples | Output the name and value of all the session attributes | ||||
|
equalsAttribute | Availability: 1.0 | ||||
Includes the body of the tag if the session attribute value equals the match attribute. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
name | Yes | No | 1.0 | ||
Name of session attribute. |
|||||
value | No | No | 1.0 | ||
If value is set to false the equalsAttribute tag includes body of tag if session attribute does not equal the match attribute. Default is true. |
|||||
match | Yes | No | 1.0 | ||
String to test for equality with session attribute value. |
|||||
ignoreCase | No | No | 1.0 | ||
Set to true to test for equality while ignoring the case of the letters. Set to false to test for case sensitive equality. Default is false |
|||||
Variables | None | ||||
Examples | Determine if the session attribute "test1" value equals "blah" | ||||
|
existsAttribute | Availability: 1.0 | ||||
Includes the body of the tag if the session attribute exists. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
name | Yes | No | 1.0 | ||
Name of session attribute. |
|||||
value | No | No | 1.0 | ||
If value is set to false the existsAttribute tag includes body of tag if session attribute does not exist. Default is true. |
|||||
Variables | None | ||||
Examples | Determine if the "test1" Session attribute exists | ||||
|
removeAttribute | Availability: 1.0 | ||||
Removes an attribute from a session. |
|||||
Tag Body | empty | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
name | Yes | No | 1.0 | ||
Name of session attribute to remove. |
|||||
Variables | None | ||||
Examples | Remove the session attribute with name "test1" | ||||
|
setAttribute | Availability: 1.0 | ||||
Sets the value of the session attribute to the content of the tag body. |
|||||
Tag Body | JSP | ||||
Restrictions |
None |
||||
Attributes | Name | Required | Runtime Expression Evaluation | Availability | |
name | Yes | No | 1.0 | ||
Name of session attribute to set value for. |
|||||
Variables | None | ||||
Examples | Set the session attribute with name "test1" | ||||
|
See the example application session-examples.war for examples of the usage of the tags from this custom tag library.
Java programmers can view the java class documentation for this tag library as javadocs.
Review the complete revision history of this tag library.