View comments | RSS feed

Using JSP tags and tag libraries

You can use JSP tags from any JSP tag library. For example, you can use any of the custom tags in the open-source Apache Jakarta Project Taglibs project tag libraries, located at http://jakarta.apache.org/taglibs/index.html. This project consists of a number of individual JSP custom tag libraries for purposes ranging from JNDI access to generating random text strings.

Using a JSP tag in a ColdFusion page

JSP pages use a standard set of tags, such as jsp:forward and jsp:include. You can also import custom JSP tag libraries into a JSP application. You can use both the standard JSP tags and custom JSP tags in ColdFusion pages, as the following sections describe.

Standard JSP tags and ColdFusion

ColdFusion tags provide equivalent features to most standard JSP tags. For example, the cfapplet tag provides the same service as the jsp:plugin tag, and cfobject tag lets you use JavaBeans, as does the jsp:usebean tag. Similarly, you do not use the jsp:getproperty tag because ColdFusion automatically gets properties when you reference them. Therefore, ColdFusion does not support the use of standard JSP tags directly.

However, two standard JSP tags provide functionality that is useful in ColdFusion pages: the forward and include tags invoke JSP pages and Java servlets. The PageContext object described in "About GetPageContext and the PageContext object." has forward and include methods that provide the same operations. For more information about using these methods see "Accessing a JSP page or servlet from a ColdFusion page".

Using custom JSP tags in a ColdFusion page

Follow these steps to use a custom JSP tag on a ColdFusion page:

To use a custom tag:

  1. Put the tag library, consisting of the taglibname.jar file, and the taglibname.tld file, if one is supplied, in the web_root/WEB-INF/lib directory.
  2. In the ColdFusion page that uses a JSP tag from the tag library, specify the tag library name in a cfimport tag; for example:
    <cfimport taglib="/WEB-INF/lib/random.jar" prefix="random">
    

    If the TLD file is not included in the JAR file, use the .tld suffix in place of the .jar suffix.

    Note:   The cfimport tag must be on the page that uses the imported tag. You cannot put the cfimport tag in Application.cfm.

  3. Use the custom tag using the form prefix:tagName; for example:
    <random:number id="myNum" range="000000-999999" />
    

Note:   You cannot use the cfsavecontent tag to suppress output of a custom JSP tag.

Example: using the random tag library

The following example uses the random tag library from the Apache Jakarta Taglibs project and calls the library's number tag, which initializes a random number generator that uses a secure algorithm to generate a six-digit random number. You get a new random number each time you reference the variable randPass.random.

<cfimport taglib="/WEB-INF/lib/random.jar" prefix="myrand">
<myrand:number id="randPass" range="000000-999999" algorithm="SHA1PRNG" provider="SUN" />
<cfset myPassword = randPass.random>
<cfoutput>
  Your password is #myPassword#<br>
</cfoutput>

For more information on the Jakarta random tag library and how to use its tags, see the documentation at the Apache Jakarta Taglibs project website, http://jakarta.apache.org/taglibs/index.html. The Taglibs project includes many open source custom tag libraries.

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


jefftapper said on Oct 23, 2002 at 12:24 AM :
I cant seem to make this cfimport example work. I've downloaded the latest random.jar and random.tld from jakarta, and dropped it into the /WEB-INF/lib/ directory, but when I run it, I get the error message:
"The TagExtraInfo class org.apache.taglibs.random.RandomNumTei for the number tag couldn't be found. "

Can anyone point me in the right direction?

Here is the code I'm using:
<cfimport taglib="/WEB-INF/lib/random.jar" prefix="myrand">
<myrand:number id="randPass" range="000000-999999" algorithm="SHA1PRNG" provider="SUN" />
<cfdump var="#randPass#">

<cfset myPassword = randPass.random>
<cfoutput>
Your password is #myPassword#<br>
</cfoutput>
jringold said on Oct 31, 2002 at 2:38 AM :
I get the same error, Win2000 Advanced Server, ColdFusion MX Professional.
hlichtin said on May 27, 2003 at 9:49 AM :
replace the import line with
<cfimport taglib="/WEB-INF/lib/taglibs-random.jar" prefix="myrand">

(the only change is to change random to taglibs-random.) Make sure you didn't rename the jar file from the original taglibs-random. This works with the latest taglibs distribution on Win 2000.
amccollough said on Feb 17, 2004 at 2:03 PM :
Did that, and I get the same error these folks are mentioning. Using W2k3 Ent Ed. CFMX 6.1 Std. Ed.
halL said on Feb 18, 2004 at 8:22 AM :
The following code should work:

<cfimport taglib="/WEB-INF/lib/taglibs-random.jar" prefix="myrand">
<myrand:number id="randPass" range="000000-999999" algorithm="SHA1PRNG" provider="SUN" />
<cfset myPassword = randPass.random>
<cfoutput>
Your password is #myPassword#<br>
</cfoutput>

Before you run the code, try restarting ColdFusion.
If that fails, try copying the random.tld file from the JAR file into the lib directory, and then restart ColdFusion again.
Moonscape9 said on Jul 3, 2004 at 8:52 AM :
I got this example to work using CFMX Enterprise Edition v6.1 on a Windows 2000 server using the following setup:

1. Place the “taglibs-random.jar” JAR file in the following directory: C:\CFusionMX\wwwroot\WEB-INFib
(assumes CFMX is installed on the C drive)

2. Place the “taglibs-random.tld” TLD file in the same directory your .cfm page that calls it. (note: you may place this file in any directory as long as you path to it correctly.)

3. Restart ColdFusion Services

4. Use the following code in your (.cfm) page:
(note that the referenced “taglibs-random.tld” is a TLD note the JAR file)

<cfimport taglib="taglibs-random.tld" prefix="myrand">
<myrand:number id="randPass" range="000000-999999" algorithm="SHA1PRNG" provider="SUN" />
<cfset myPassword = randPass.random>
<cfoutput>
Your password is #myPassword#<br>
</cfoutput>

OTHER IMPORTANT NOTES:

JSP custom tags is NOT supported in CFMX Professional Edition - Enterprise only and might also work on the CFMX Demo Install and CFMX Developer version.

CFIMPORT must appear in the page using a custom tag

You can’t place the CFIMPORT code in the Application.cfm file.

You can’t place the CFIMPORT code in a file referenced with CFINCLUDE.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/Java3.htm