Incorporating Java into your ColdFusion Applications
For any ColdFusion developer, ignoring Java is a bad career strategy. It is the undisputed 800-lb gorilla of software languages, having wrested that title from C++. Even Macromedia, a company with a language that might fairly be assumed to be in competition with the new champ, is sounding the drums for Java and for all things object-oriented (OO). A quick look at the articles on macromedia.com/desdev will confirm this. As developers, we ignore Java at our own peril.
Some advocate simply throwing ColdFusion over for Java, "Adapt or Perish" being their motto. I think this stratagem ignores the relative strengths of both languages and in this newsletter, I'd like to show how the two can work together nicely, leveraging the excellent features that both languages offer.
Why Java? Apart from the undeniable demand for Java programmers (the highly respected Gartner Group recently issued a report outlining an "object crisis" due to lack of Java programmers), Java deserves our consideration because it excels at handling the complex, difficult programming usually associated with enterprise-level applications. Learning Java is excellent "career obsolescence insurance".
Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors
|
|
Why ColdFusion? ColdFusion is unsurpassed at speed of development and at ease of use. Where Java is complex, ColdFusion is simple-a virtue often overlooked in foolish buzzword wars. The great majority of applications are not enterprise-level and don't need the sophistication-and complexity-of Java.
Putting the two languages together offers great benefits and is surprisingly easy. Let me illustrate: say that we have a Java back-end system that handles processing of orders and that we are building a ColdFusion front end for. The trouble is getting the information out of the Java system. If the thought makes you break out into a cold sweat, know that you're not alone. But the reality of making the two work together is actually quite simple.
Java programs-and OO programs in general-work by means of sending messages to objects. These messages have corresponding functions(methods in OO-speak). This is the central idea behind OO languages and together these methods give us a very clear means of accessing Java applications.
In our hypothetical order-processing Java program, we might have a class called OrderManager with a method called newOrder(). Here is some Java code, greatly simplified for this explanation:
public class OrderManager{
private int orderNumber = 1000;
public int newOrder(){
return ++this.orderNumber;
}
}
The method, newOrder(), increments the last orderNumber and returns this information. Once this Java file is compiled, we can make immediate use of it in our ColdFusion application:
<cfset orderMgr = CreateObject( 'java', OrderManager' )>
<cfset orderMgr.init()>
<cfset newOrderNumber = orderMgr.newOrder()>
<cfoutput>
<h2>New Order Form</h2>
<h3> Order Number #newOrderNumber# </h3>
<form action="OrderProcessor.cfm" method="post">
<input type="Hidden" name="order_number" value="#newOrderNumber#">
<!--- Place additional form code here --->
</form>
</cfoutput>
With those few lines of code, we have called the Java OrderManager class, creating a new object and calling the newOrder() method, which returned the latest order number. Our form processing page could then call another OrderManager method-updateOrder(), perhaps-sending to it the arguments needed to create a new order in the back-end order system. We let Java do what it is good at-handling complex business logic-and let ColdFusion do what it is good at-rapidly creating an application based on this logic.
You can try this out on your own machine. To make it work, you'll need a two things: the compiled Java class file, OrderManager.class and the Java SDK (you can download this from java.sun.com). In the ColdFusion administrator, you'll need to provide a "class path" definition, pointing to where the Java class file can be found. If you want to try it out, come to www.halhelms.com and click the link for "This month's lesson". The Java file you'll need can be downloaded. Also, you'll see detailed instructions on setting up Java. It's really not difficult. Within a very few minutes, you'll have ColdFusion talking to Java!
If you're interested in learning more about Java-and about integrating ColdFusion and Java-I'm giving a class called "Java for ColdFusion Programmers" in Orlando, immediately following Macromedia's DevCon. This is a five-day class Oct. 31-Nov 4, Thursday through Monday and has been designed to help you leverage your ColdFusion skills to learn Java. I find this is one of the best ways for ColdFusion programmers to approach Java, and by the end of the class, you'll be able to create real applications in Java and connect to them using ColdFusion. I hope to see you there!
|