Welcome!

ColdFusion Authors: Mike Kavis, Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

How May I Be of Web Service?

How May I Be of Web Service?

There is a phenomenon that appears to be unique to our industry - something I call buzzword du jour. On a regular basis a new buzzword (usually accompanied by a series of acronyms) appears on the front pages of the trade rags proclaiming to be "the future" - the message being "jump on board or become obsolete." This happens so frequently that many of us are becoming immune to it all, and in doing so are running the risk of missing something important when (or if) it happens. Which brings us to Web services.

What's the Deal?
If you are to believe the hype, Web services are more exciting than car chases, are the answer to world peace, can cure male-pattern baldness, will patch the ozone layer, can resurrect failing economies, will make you rich and desirable, may bring an end to the glut of reality TV shows... well, you get the idea.

So, the big question has to be, are Web services for real? Microsoft thinks so (but not everything Microsoft believes in is truly worthy of consideration - anyone remember Microsoft Bob?). Sun (thanks to applets) thinks so too, as does IBM (those folks who brought us OS/2, which did so well). The point is, just because the big guys say it's hot does not necessarily make it so. And yet everyone is talking and writing about Web services (and I myself must plead guilty here - I have mentioned Web services in passing in several columns since ColdFusion MX shipped). So, again, what's the deal?

It's Distributed Applications - Again
For starters, here's an explanation. Web services are simply a set of technologies that facilitate distributed computing; it is that simple. The idea is that an application can invoke another application (located anywhere, locally or remotely) which does some processing, possibly returning results when complete. That's all there is to it.

It's not a new idea. Distributed computing has been around in some way, shape, or form for a while now (heck, if you use ColdFusion's <CFHTTP> tag to invoke a remote page, you are using distributed computing). So why the excitement this time around? There are a few reasons:

  • Vendor support: It's been a while since everyone from Microsoft to Sun to IBM to Apple all talked the same language (for the most part). Web services are truly the first really viable solution to interoperability.
  • Standards: Unlike other distributed computing technologies, Web services are built on existing standards and systems; no single vendor owns the space.
  • Cost: The two prior bullets make this technology highly usable and accessible, and not at all cost prohibitive. Most developers can continue to use the platforms and languages they already use and don't need to throw out everything and start over.

    Need a practical example? Try this one: you create an online store (I know that is so '90s, but bear with me). Customers visit your site and buy goods that you ship, using a carrier of your choice. Great. That is until a week goes by and no package shows up, so the customer revisits your site to track the order. You display the order information, complete with the shipper information and tracking number, allowing the customer to check on the delivery (and maybe, as you are so customer-service driven, you even provide a direct link to the shipper's site). Problem solved. Right?

    Wrong! If you have an online store, the last thing you want is customers wandering off to other sites; you want them to stay and part with their hard-earned money. You never want to send them off to fedex.com or ups.com or any other site. Amazon.com doesn't do this - they show shipping information provided by those companies right in their own pages, the customer goes nowhere.

    I have no idea what systems the shipping companies use, I don't know what software they use, and I have no access to it anyway (I am guessing the same is true for you). Amazon.com is a very significant player in the e-commerce space, the shipping companies are likely to work with them to provide them with access to this kind of data. But what about the rest of us? How could we do the same?

    The answer is Web services (predictable, I know, sorry). If the shipping companies created a Web service that exposed tracking information, any application could submit a real-time request for the data, and then process or display it as needed right within their own pages. In other words, the client browser talks to your application; your application talks to the shipper via their Web service, retrieving the necessary information; and then your application displays it as needed.

    Fantasy? Well, that depends on who you ship your products with. But some shipping companies have indeed made Web services available. It's not fantasy at all.

    Web Services Lingo
    Okay, so now that you know why you need Web services, let's review some of the lingo you'll need to use (drop these in conversations to sound like a seasoned pro):

  • Web service: This is the application itself.
  • XML: Web service requests are made in XML and returned data is also in XML.
  • SOAP (Simple Object Access Protocol): The XML-based protocol used to send and receive Web service requests.
  • WSDL (Web Services Description Language): An XML language used to document Web services; every Web ser-vice has an associated WSDL URL which lists the methods (functions) that may be invoked as well as what parameters they expect and what data is returned.
  • UDDI (Universal Description, Discovery and Integration [of Web services]): A directory technology used to locate Web services (UDDI is not supported by ColdFusion in CFMX).

    When you use a Web service you consume it, and when you create a Web service you publish it.

    Consuming Web Services
    Enough talk, let's try this thing. In ColdFusion, Web services are consumed using the <CFINVOKE> tag. <CFINVOKE> requires the URL to a Web service's WSDL, the name of the method to invoke (a single Web service may expose multiple methods), and any required parameters.

    The following is a simple example. It invokes a Web service on my own Web site - this one returns my ColdFusion Tip-of-the-Day:

    <!--- Get tip for today --->
    <CFINVOKE WEBSERVICE="http://www.forta.com/cf/tips/syndicate.cfc?wsdl"
    METHOD="Get"
    DATE="#Now()#"
    RETURNVARIABLE="DailyTip">

    As you can see, the Web service itself is specified using the URL to its WSDL file. ColdFusion retrieves and reads this file so as to know how to interact with the Web service, and how to call the method specified in the METHOD attribute. RETURNVARIABLE is the name of a ColdFusion variable to be populated with whatever results are returned. In this example my Web service returns a ColdFusion structure, and so DailyTip will be a structure containing the tip.

    If you want to try this example you can use <CFDUMP> to display the results, like this:

    <CFDUMP VAR="#DailyTip#">

    You'll notice that there is no XML here, no SOAP, nothing beyond a simple tag call. ColdFusion hides all the complexity from you - it does not matter what is on the other end, what language it was written in, what data type it returns, what platform it runs on - none of that matters. All that is important is that the returned data is ready to use just like any other ColdFusion variable.

    Here is another example, the same Web service call, but this time using a slightly different calling convention:

    <!--- Get tip for today --->
    <CFINVOKE WEBSERVICE="http://www.forta.com/cf/tips/syndicate.cfc?wsdl"
    METHOD="Get"
    RETURNVARIABLE="DailyTip">
    <CFINVOKEARGUMENT NAME="date"
    VALUE="#Now()#"/>
    </CFINVOKE>

    As you can see, arguments may be specified as name=value pairs in <CFINVOKE>, or passed to nested <CFINVOKEARGUMENT> tags - the choice is yours, the end result is the same.

    Let's look at a complete example. Alta Vista has a translation engine named Babel Fish - pass it text and a destination language and it returns the translated text to you. The Babel Fish service requires two pieces of information, the text to be translated, and the translation mode (from what to what, for example en_de to translate English to German). See Listing 1 for a complete working application. It prompts for text and a destination language and displays the translation.

    As you can see, using Web services in ColdFusion is easy. I have no idea what the Babel Fish Web service is written in or what platform it runs on, nor do I care - the important thing is that I can invoke it and it just works. And that is the beauty of Web services.

    Note: Looking for a Web service to experiment with? Try www.xmethods.net.

    If you are using Dreamweaver MX you'll find that Web services are even easier to use. In Dreamweaver, open the Application window, click on the Component tab, and then change the dropdown to Web services. Dreamweaver will list any known Web services (the list will be empty if this is the first time you are doing this). Click the + button and you'll be prompted for the WSDL URL of a Web service - simply enter it, and click OK. Dreamweaver MX will retrieve the WSDL, process and parse it, and will then display a tree of Web service methods and arguments. Then all you need to do is drag the desired method into your ColdFusion code, and Dreamweaver will create the <CFINVOKE> call for you automatically.

    Publishing Web Services
    Now that you know how to consume Web services, let's look at how to create and publish them. ColdFusion MX makes creating Web services incredibly easy - they are created as ColdFusion Comp-onents (explained in detail in CFDJ, Volume 4, issues 6, 7, and 10).

    Here is a simple ColdFusion Component. The file is named random.cfc, and it contains two methods: (1) Get returns a random number and (2) GetRange returns a random number within the specific range. (Okay, not the most glamorous examples, but they'll have to do). See Listing 2 for the code.

    The CFC code itself is rather simple. Get accepts no arguments and returns whatever is generated by Rand(); GetRange() accepts two required numeric arguments and passes them to RandRange() returning the results. This CFC can be used within your ColdFusion code using <CFINVOKE>, <CFOBJECT>, or CreateObject().

    So what makes this a Web service? The answer is the ACCESS level. Each of the <CFFUNCTION> tags has ACCESS="remote". Any ColdFusion Component methods with an ACCESS level of remote can be accessed remotely - as Web services; it's as simple as that.

    What about the WSDL file you need? ColdFusion does that too. The WSDL URL is the URL to the CFC with ?wsdl appended to it. I saved my random.cfc in my test directory, so the WSDL URL is http://localhost:8500/test/random.cfc?wsdl.

    Listing 3 is sample code showing this CFC being used as a Web service.

    Of course this particular CFC could be invoked as a CFC rather than as a Web service (and probably should be as it is ColdFusion and it is local). But with no extra work, the ColdFusion Component can also be used by:

    • Other ColdFusion servers
    • .NET
    • Java
    • PHP
    • Client-side Flash
    ColdFusion, and CFCs in particular, makes publishing Web services painless and even fun. You don't have to worry about XML, SOAP, data-type conversions - ColdFusion does all that for you. In fact, as I demonstrated at DevCon in Orlando, you'll find it easier to create Web services for Java and .NET in ColdFusion than in Java or any .NET language!

    Conclusion
    Web services are all the rage right now. While the hype will die down a bit, all indications are that this is an important technology and one that will play an important role in application development now and in the foreseeable future. And best of all, as a ColdFusion developer, you have nothing to lose and lots to gain - it does not get much better than that.

  • More Stories By 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.

    Comments (3) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    Jon 10/02/06 03:13:39 PM EDT

    Thank you for taking the time to report on this topic. I have a question though.

    When you get errors using CFINVOKE to utilize web services and you're on a shared environment, how do you go about determining where the problem is? The error information is cryptic. The web service provider points to the programming or hosting, the host points to the service or programming and the programmer points to the host or web service provider! Of course, being the programmer, and having done several other CFINVOKE tags successfully, I am looking at the other two!

    So where to next?

    Could not generate stub objects for web service invocation. Name: http://app.campaignmonitor.com/api/api.asmx?WSDL. WSDL: http://app.campaignmonitor.com/api/api.asmx?WSDL. org.apache.axis.wsdl.toJava.DuplicateFileException: Duplicate file name: C:\CFusionMX7\stubs\WS1938286156\com\campaignmonitor\app\api\SubscriberUnsubscribe.java. Hint: you may have mapped two namespaces with elements of the same name to the same package name.

    Jon 10/02/06 03:12:48 PM EDT

    Thank you for taking the time to report on this topic. I have a question though.

    When you get errors using CFINVOKE to utilize web services and you're on a shared environment, how do you go about determining where the problem is? The error information is cryptic. The web service provider points to the programming or hosting, the host points to the service or programming and the programmer points to the host or web service provider! Of course, being the programmer, and having done several other CFINVOKE tags successfully, I am looking at the other two!

    So where to next?

    Could not generate stub objects for web service invocation. Name: http://app.campaignmonitor.com/api/api.asmx?WSDL. WSDL: http://app.campaignmonitor.com/api/api.asmx?WSDL. org.apache.axis.wsdl.toJava.DuplicateFileException: Duplicate file name: C:\CFusionMX7\stubs\WS1938286156\com\campaignmonitor\app\api\SubscriberUnsubscribe.java. Hint: you may have mapped two namespaces with elements of the same name to the same package name.

    jim kellow 01/16/03 08:25:00 AM EST

    Thanks for the time you spent on this.
    Like others, I have seen web services
    in articles but passed over it as I
    didn't have a clue what it was about.
    A couple of years ago, my company
    jumped into the web world with both
    feet and I rushed to create the site,
    but since then the interest has fallen
    to zero. I haven't been involved in the
    CF world for over a year now(we still
    use cf 4.5).
    I tried pushing for us to convert
    some of our existing application
    jobs to CF but it turned out that the
    conversion is about 10+ CF jobs to replace 1 application(oracle forms).
    I am trying to get us back into the
    CF world, but its gotten a lot bigger
    since we were there. I have a new
    copy of CF 5.0 on my desk but it is
    way out of date, so I am going to have
    to figure out which version of CF to
    suggest we move up to. Again, thanks
    for the article.

    JimK