Download Extending ColdFusion with CFX
Transcript
Introducing the CFX API Another reason to use C++ is the fact that it is generally thought to be faster at extremely CPUintensive tasks. However, recent advances in Java Virtual Machine technology have made Java code more likely than ever to perform nearly as well as comparable C++ code. Also, the ColdFusion server can invoke Java CFXs directly, but must go through an additional layer (the Java Native Interface, or JNI) when invoking a C++ tag. In my opinion, sheer performance issues should only guide you toward C++ in extreme situations. All things considered, I recommend that you go with Java if you really have a choice between the two. The code will probably be faster, easier to write, and will almost certainly be easier to debug. Even if you don’t anticipate using to use the tag on multiple platforms now, it will still be nice to know that your choice of Java keeps that option open for the future. Introducing the CFX API Regardless of the language you use to create a CFX tag, you need a way to pass information back and forth between the tag code and the ColdFusion page that is using the tag. You also need ways to safely throw exceptions (errors), create query objects, and include output in the current Web page. Macromedia defines a set of objects and functions that provides all this functionality and more. The objects and functions are known collectively as the CFX API. The following sections will introduce you to the various functions included in the CFX API, organized by subject. Within each section, the Java syntax will be shown in one table, followed by the C++ syntax in a second table. You will notice that the Java and C++ versions of the API are very similar. For the most part, there is a one-to-one relationship between the methods provided by both versions of the API. This is great because once you design a few tags in Java, you will be well on your way to understanding what’s possible with C++, and vice versa. Working with the Tag’s Attributes Like native ColdFusion tags or CFML custom tags, your CFX tags can accept any number of attributes. You can easily add code to your tag so that some of its attributes are required while others remain optional. The CFX API defines several functions that a tag can use to accept and validate attributes from the calling ColdFusion page. Table 30.2 shows the Java version of these functions, and Table 30.3 shows the C++ version. Table 30.2 Java Methods for Working with Attributes METHOD DESCRIPTION request.attributeExists(name) Determines whether the specified attribute was actually passed to the CFX tag. Returns a boolean value. request.getAttribute(name) Returns the value of the specified attribute, as it was passed to the CFX tag. Note that all attribute values are received by the CFX as String values. request.getAttributeList(name) Returns the names of all attributes that were actually passed to the CFX tag. The names are returned as an array of strings. 3