Accessibility

Table of Contents

A Beginner's Guide to Creating and Consuming Web Services with ColdFusion and Flash

Creating a Web Service with ColdFusion

If you know how to create a function in ColdFusion, or any programming language for that matter, you have a solid understanding of how to create a web service in ColdFusion. Basically, in ColdFusion you create a web service by writing a function and wrapping it in a special tag. So now you're ready to see how it's done!

The steps for creating a web service in ColdFusion are:

  1. Create a ColdFusion component (CFC).
  2. Create a function in the CFC that performs the processing for the web service.
  3. Set the access attribute of the function to a value of remote.

Creating a Simple Web Service

The first web service you will build returns some text to the consumer of the web service. First, you create a document that uses the cfcomponent tag. ColdFusion components have many uses, one of which is acting as a wrapper tag for functions that provide the application functionality you want your web service to provide. So, to build a web service, you must create a document and place opening and closing cfcomponent tags in the document.

Next, you insert function with the cffunction tag, inside the cfcomponent tag. Specify three attributes for your function. They are:

  1. name: This is the name of the function and is also used when calling the web service.
  2. returntype: This specifies the data type of the information that is returned by the web service. If no data is returned, set the value of returntype to void.
  3. access: This specifies how the function can be used. Set the value of this attribute to remote for the function to be used as a web service.

The simple web service is going to return a string. To return values in a ColdFusion function you use the cfreturn tag.

Now you are going to actually build the web service yourself.

Exercise 1

  1. Create a new document and save it as Simple.cfc in C:\CFusionMX\devnet\ws.

    Note: There is a reason the file name started with an uppercase letter. CFCs are ColdFusion's implementation of parts of object-oriented programming, and you are creating a class with the CFC. Best practices in OOP say that class names should be uppercase.

  2. Add the opening and closing cfcomponent tags.
  3. Nest opening and closing cffunction tags within the cfcomponent tags.
  4. In the cffunction tag, add the following attributes:

    • name:firstws
    • access:remote
    • returntype:string
  5. Within in the cffunction tags, nest a cfreturn tag to return the following text: Developer Center is great!
  6. Ensure your code appears as shown here:
    <cfcomponent>
    	<cffunction name="firstws" access="remote" returntype="string">
    		<cfreturn "Developer Center is great!">
    	</cffunction>
    </cfcomponent>
    
  7. Save the document.