Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Java For CFers - Part 3
Java For CFers - Part 3

In my last two columns we looked at JavaServer Pages (JSP) and compared ColdFusion development to JSP development. As explained, JSP is designed to dramatically lower the cost of entry into the Java development world, and JRun, in particular (with its tag libraries), does even more to deliver on this promise.

We ColdFusion developers know that a CF-Java convergence is on the way. We also know that when this happens, knowledge of Java won't be required and we'll be able to develop CF code as we do now. However, as explained earlier in this series, there are compelling reasons to consider Java-based development and the Java platform - even for ColdFusion developers and even right now.

Why Leverage Java?
It's no secret - Java is hot, and the sheer mass of available Java re-sources is nothing short of mind-boggling. From books and tutorials, to commercial and publicly available libraries, to interfaces for all sorts of applications and products - the ability to interact with Java opens up all sorts of new avenues for ColdFusion developers.

In addition and as I've written in previous columns, ColdFusion developers need to think about code reuse, abstraction, encapsulation, and componentization. As the next generation of ColdFusion will be built upon a Java platform, using Java components within your code ensures that you're walking the same path Allaire is.

All said and done, ColdFusion developers need to take a good look at Java - right away.

Integrating ColdFusion and Java
ColdFusion provides three ways to interact with Java:

  1. The CFX interface (originally designed to allow C/C++ developers to extend CF) supports Java. Using this interface (CFAPI) you can write Java code that can be registered within ColdFusion, then used like any other tag.
  2. <CFSERVLET> can be used to execute a servlet in the Java server. This tag is used in conjunction with <CFSERVLETPARAM>, which passes data to the servlet. Data re-turned by <CFSERVLET> may be written out to the client or saved to a variable for further manipulation.
  3. <CFOBJECT> (as of ColdFusion 4.5) supports Java objects and Enterprise JavaBeans (EJB). The function equivalent of <CFOBJECT>, CreateObject(), also supports Java.

Of these three options <CF-SERVLET> is, for all intents and purposes, the least useful. You'll seldom want to display servlet results as is (if you did, you'd probably have used the servlet directly); if you need to manipulate the output, you can use <CFHTTP> to call the page.

For leveraging Java within your ColdFusion code <CFOBJECT> is probably the most useful in that it allows you to interact with generic Java (unlike the CFX interface that requires CF-specific Java coding). However, the CFX interface does have an advantage - it's far more CF aware (for example, it can read and write queries).

Java via <CFOBJECT>
<CFOBJECT> was introduced in ColdFusion 3 as a means to connect to COM. The tag was extended in ColdFusion 4 to support CORBA, and was extended again in ColdFusion 4.5 to support Java.

The following code snippet (based on example code that's included with ColdFusion) invokes Java code (see Listing 1) that creates a JPEG image containing user-supplied text:

<CFOBJECT TYPE="JAVA" ACTION="Create" NAME="image" CLASS="HelloWorldGraphic">
<CFSET imglen=image.createImage("Hello Ben")>

<CFOBJECT> requires that the Java class is specified along with a name to refer to the instance. That name is then used to pass attributes and invoke specific methods.

<CFSCRIPT> can also be used to invoke Java code, and the syntax this way is even cleaner:

<CFSCRIPT>
image=CreateObject("Java", "Hello WorldGraphic");
imglen=image.createImage("Hello Ben");
</CFSCRIPT>

As you can see, calling Java code isn't difficult at all. As long as you know the names of the required methods and attributes it's just a matter of calling <CFOBJECT> and <CFSET> (or their <CFSCRIPT> counterparts) as needed.

Custom Tag Wrappers
The examples I used here are rather simplistic - you'll find invoking Java code in real world applications is typically more involved. Ideally, you'll want to write the interface code just once, and custom tags are a perfect way to do this. The following custom tag encapsulates the above code to simplify its use:

<CFPARAM NAME="ATTRIBUTES.message" DEFAULT="">
<CFSCRIPT>
image=CreateObject("Java", "Hello-WorldGraphic");
imglen=image.createImage("#ATTRI-BUTES.message#");
</CFSCRIPT>
Now invoking Java code is as complex as this:
<CF_HelloWorldGraphic MESSAGE="Hello Ben">

Summary
Java is an important part of ColdFusion's future, and you'll be hearing lots more about it (and how it affects us CFers) in the next few months. For now, I strongly recommend you look at JRun (you can download the Developer's Edition from the Allaire Web site, www.allaire.com), and when appropriate, consider using Java as a way to extend ColdFusion - together they form the ultimate development platform.

About Ben Forta
Ben Forta is Adobe's Senior Technical Evangelist. In that capacity he spends a considerable amount of time talking and writing about Adobe products (with an emphasis on ColdFusion and Flex), and providing feedback to help shape the future direction of the products. By the way, if you are not yet a ColdFusion user, you should be. It is an incredible product, and is truly deserving of all the praise it has been receiving. In a prior life he was a ColdFusion customer (he wrote one of the first large high visibility web sites using the product) and was so impressed he ended up working for the company that created it (Allaire). Ben is also the author of books on ColdFusion, SQL, Windows 2000, JSP, WAP, Regular Expressions, and more. Before joining Adobe (well, Allaire actually, and then Macromedia and Allaire merged, and then Adobe bought Macromedia) he helped found a company called Car.com which provides automotive services (buy a car, sell a car, etc) over the Web. Car.com (including Stoneage) is one of the largest automotive web sites out there, was written entirely in ColdFusion, and is now owned by Auto-By-Tel.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE