User Guide

280 Developing Web Applications with ColdFusion
To create a Java CFX:
1. Create a new source file in your editor.
2. Enter the code, for example, the code below illustrates the creation of a very
simple Java CFX named SimpleJavaCFX that writes a text string back to the calling
page:
import com.allaire.cfx.* ;
public class HelloColdFusion implements CustomTag
{
public void processRequest( Request request, Response response )
throws Exception
{
String strName = request.getAttribute( "NAME" ) ;
response.write( "Hello, " + strName ) ;
}
}
3. Save the file as HelloColdFusion.java in the classes subdirectory
4. Compile the java source file into a class file using the java compiler. If you are
using the command line tools bundled with the JDK, you do this using the
following command line, which you execute from within the
classes directory:
javac -classpath cfx.jar HelloColdFusion.java
Note The above command will only work if the java compiler (javac.exe)
is in your path. If it is not in your path, specify the fully qualified
path, for example:
c:\jdk12\bin\javac on Windows NT, or /usr/java/bin/javac on Solaris
If you receive errors during compilation, check the source code to make sure you have
entered it correctly. If no errors occur, you have just successfully written your first Java
CFX!
As you can see, implementing the basic CustomTag interface is very straightforward.
This is all a Java class has to do to be callable from a CFML page.
Processing Requests
Implementing a Java CFX requires interaction with the Request and Response objects
passed to the
processRequest method. In addition, CFXs that need to work with
ColdFusion queries will also interface with the
Query object. The com.allaire.cfx
package, located in the classes/cfx.jar archive contains the Request, Response, and
Query objects.
A basic overview of each of these object types is provided below. To see a complete
example Java CFX that uses Request, Response, and Query objects, see the “ZipBrowser
Example” on page 284.