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

For more information, please explore the Attic.

Jakarta Project: Random Tag library

Version: 1.0.2

Table of Contents

Overview

The Random tag library creates random strings and random numbers. You may specify a set of allowable characters as well as the length for randomly generated strings. You may specify a range of allowable integers or a random floating point number between zero and one for randomly generated numbers.

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/random-1.0" prefix="rand" %>

where "rand" 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

Random Tags
string Set the properties of a variable length random string generator. A new random string will be created every time the jsp:getProperty tag is used to access the generator.
number Set the properties of a random number generator. A new random number will be generated each time the jsp:getProperty tag is used to access the generator .
 

Tag Reference

string Availability: 1.0

Set the properties of a variable length random string generator. A new random string will be created every time the jsp:getProperty tag is used to access the generator.

Tag BodyEmpty    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

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

 length  No   No  1.0
 

The length of the random string. Defaults to a length of 8.

 map  No   No  1.0
 

Attribute name of a Bean or Script variable which is a Java HashMap object. The random string generated will be a new unique key for use as a key for the HashMap.

 charset  No   No  1.0
 

The set of characters to use when creating the random string. The following characters can be used as a charset a-zA-Z0-9 ! @ # $ % ^ & * ( ) _ \- + = { } [ ] \\ | : ; \" ' < > , . ? / The backslash, hyphen, and double quotes characters must be escaped with a \: like this \\, \-, and \". Lowercase alpha characters can be specified using a range like "a-z" or "f-r", the same can be done with uppercase letters A-Z and digits 0-9. To use the entire character set, set charset="all". Single characters must be separated by a space. Charset defaults to a value of a-zA-Z0-9.

 algorithm  No   No  1.0
 

The algorithm to be used if a SecureRandom object is going to be used to generate the random string.

 provider  No   No  1.0
 

The package to search for the desired algorithm that is to be used for a SecureRandom object.

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

Name used to retrieve the random string later in the page.

 PropertiesNameGetSetAvailability
   random Yes No 1.0
  

The variable length randomly generated string from the named generator.

Examples Create a random string that is 8 characters long and that uses a charser of all characters.  
 


 
<rand:string id="random1" charset="all"/>
       
          
 
<jsp:getProperty name="random1" property="random"/>
       
          
 
<rand:string id="random2" charset="A-Z"/>
       
          
 
<jsp:getProperty name="random2" property="random"/>
       
          
 
<rand:string id="random3" charset="A-Z" length="15"/>
       
          
 
<jsp:getProperty name="random3" property="random"/>
       
          
 
<rand:string id="random4" charset="A-Z ! @ # \\ \- + ? &amp; $"/>
       
          
 
<jsp:getProperty name="random4" property="random"/>
       
          
 
<rand:string id="random5" map="passkeys"/>
       
          
 
<jsp:getProperty name="random5" property="random"/> 
       
          
 
<rand:string id="random6" algorithm="SHA1PRNG" provider="SUN"/>
       
          
 
<jsp:getProperty name="random6" property="random"/> 
       
          

number Availability: 1.0

Set the properties of a random number generator. A new random number will be generated each time the jsp:getProperty tag is used to access the generator.

Tag BodyEmpty    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 id  Yes   No  1.0
 

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

 range  No   No  1.0
 

This parameter determines the lower and upper bound from which to select the random number. If a range of 0-1 is selected, the tag will automatically create a float value between 0 and 1. The default range is the digits 0 to 100. Max size of the random number is limited by the size of the primitive data type of long.

 algorithm  No   No  1.0
 

The algorithm to be used if a SecureRandom object is going to be used to generate the random string.

 provider  No   No  1.0
 

The package to search for the desired algorithm that is to be used for a SecureRandom object.

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

Name used to retrieve the random number later in the page.

 PropertiesNameGetSetAvailability
   random Yes No 1.0
  

The variable length randomly generated number as a Java Number object. In a JSP page the number is printed out as a string.

Examples Create a default random number in the range 10000000-99999999.  
 

   
      
<rand:number id="random1" range="10000000-99999999"/>
               
          
      
<jsp:getProperty name="random1" property="random"/>
               
             
      
<rand:number id="random2" range="0-1"/>
               
          
      
<jsp:getProperty name="random2" property="random"/> 
               
          
 
<rand:number id="random3" algorithm="SHA1PRNG" provider="SUN"/>
       
          
 
<jsp:getProperty name="random3" property="random"/> 
       
          

Examples

See the example application random-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.