2010-04-14 - Jakarta Taglibs has been retired.

For more information, please explore the Attic.

Jakarta Project: Session Tag library

Version: 1.0.1

Table of Contents

Overview

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.

Requirements

This custom tag library requires no software other than a servlet container that supports the JavaServer Pages Specification, version 1.1 or higher.

Configuration

Follow these steps to configure your web application with this tag library:

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.

Tag Summary

Session Tags
sessionAccess general information about session.
isNewDetermine if a session is new.
invalidateInvalidate a user session and remove it.
maxInactiveInterval Set the maximum inactive interval before a session times out.
 
Session Attribute Tags
attributeGet the value of a single session attribute.
attributesLoop through all session attributes.
equalsAttribute See if a session attribute equals some value.
existsAttribute See if a session attribute exists.
removeAttributeRemoves an attribute from a session.
setAttributeSets the value of a session attribute.
 

Tag Reference

session Availability: 1.0

Access general information about session.

Tag Bodyempty    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag.

VariablesNameScopeAvailability
  id attribute value   Start of tag to end of page  1.0
 

Session Data Bean

 PropertiesNameGetSetAvailability
   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  
 



<sess:session id="ss"/>
The creation time in milliseconds since midnight January 1, 1970 GMT is:
<jsp:getProperty name="ss" property="creationTime"/>
 
          

isNew Availability: 1.0

Determine if a session is new.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired 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.

VariablesNone
Examples Display whether the clients session is new  
 


 
<sess:isNew> 
  This session is new.
</sess:isNew>
<sess:isNew value="false">
  This session is not new.
</sess:isNew>
       
          

invalidate Availability: 1.0

Invalidate a user session and remove it.

Tag Bodyempty    
Restrictions

None

AttributesNone
VariablesNone
Examples Invalidate the users session and remove it  
 

   
      
<sess:invalidate/>
               
          

maxInactiveInterval Availability: 1.0

Set the maximum inactive interval before a session times out in seconds using the content of the tag body.

Tag BodyJSP    
Restrictions

None

AttributesNone
VariablesNone
Examples Sets the session maximum inactive interval to 900 seconds  
 

   
         
<sess:maxInactiveInterval>900</sess:maxInactiveInterval>
       
          

attribute Availability: 1.0

Get the value of a single session attribute.

Tag Bodyempty    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 name  Yes   No  1.0
 

Name of session attribute to get.

VariablesNone
Examples Output the value of the session attribute with name "test1"  
 



<sess:attribute name="test1"/>

          

attributes Availability: 1.0

Loop through all session attributes.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

Script variable id for use with standard jsp:getProperty tag.

VariablesNameScopeAvailability
  id attribute value   Nested within tag  1.0
 

Session Attribute Bean

 PropertiesNameGetSetAvailability
   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  
 



<sess:attributes id="loop">
Name: <jsp:getProperty name="loop" property="name"/>
Value: <jsp:getProperty name="loop" property="value"/>
</sess:attributes>
    
          

equalsAttribute Availability: 1.0

Includes the body of the tag if the session attribute value equals the match attribute.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired 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

VariablesNone
Examples Determine if the session attribute "test1" value equals "blah"  
 



<sess:equalsAttribute name="test1" match="blah">
  The session attribute with name test1 matches the string "blah".
</sess:equalsAttribute>
<sess:equalsAttribute name="test1" match="blah" value="false">
  The session attribute with name test1 does not match the string "blah".
</sess:equalsAttribute>

          

existsAttribute Availability: 1.0

Includes the body of the tag if the session attribute exists.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired 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.

VariablesNone
Examples Determine if the "test1" Session attribute exists  
 



<sess:existsAttribute name="test1">
 The session attribute with name test1 exists.
</sess:existsAttribute>
<sess:existsAttribute name="test1" value="false">
 The session attribute with name test1 does not exist.
</sess:existsAttribute>
         
          

removeAttribute Availability: 1.0

Removes an attribute from a session.

Tag Bodyempty    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 name  Yes   No  1.0
 

Name of session attribute to remove.

VariablesNone
Examples Remove the session attribute with name "test1"  
 

   

<sess:removeAttribute name="test1"/>

          

setAttribute Availability: 1.0

Sets the value of the session attribute to the content of the tag body.

Tag BodyJSP    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 name  Yes   No  1.0
 

Name of session attribute to set value for.

VariablesNone
Examples Set the session attribute with name "test1"  
 

   

<sess:setAttribute name="test1">Test Value</sess:setAttribute>
 
          

Examples

See the example application session-examples.war for examples of the usage of the tags from this custom tag library.

Java Docs

Java programmers can view the java class documentation for this tag library as javadocs.

Revision History

Review the complete revision history of this tag library.