Creating a simple web service using ColdFusion


Creating a simple web service using ColdFusion

The fol­low­ing exam­ple shows how you can cre­ate and invoke a sim­ple web ser­vice in Cold­Fu­sion by cre­at­ing a .CFC file and using the <CFCOM­PO­NENT>, <CFFUNC­TION>, <CFIN­VOKE>, and <CFOB­JECT> tags.

First, cre­ate the fol­low­ing Cold­Fu­sion com­po­nent file, wwwroot/cfcs/Hello.cfc, within your WWW­ROOT folder:


Next, you can invoke the cus­tom com­po­nent using any of the fol­low­ing snippets:

1) #greeting#

2) #greeting#

3) #greeting#

4) #cfc.sayHello("Peter")#

cfc = createObject("component", "cfcs.Hello"); greeting = cfc.sayHello("Peter"); writeOutput("

5) " & greeting & "

greeting = createObject("component", "cfcs.Hello").sayHello("Peter"); writeOutput("

6) " & greeting & "


The following example shows how you can create and invoke a simple web service in ColdFusion by creating a .CFC file and using the <CFCOMPONENT>, <CFFUNCTION>, <CFINVOKE>, and <CFOBJECT> tags.

First, create the following ColdFusion component file, wwwroot/cfcs/Hello.cfc, within your WWWROOT folder:

<cfcomponent displayName="Simple HelloWorld component" output="false">
    <cffunction name="sayHello" access="remote" output="false" returnType="string">
        <cfargument name="nom" required="true" type="string" />
        <cfreturn "Hello, " & trim(arguments.nom) & "." />

Next, you can invoke the custom component using any of the following snippets:

<cfinvoke component="cfcs.Hello"
        returnVariable="greeting" />
<cfoutput><p>1) #greeting#</p></cfoutput>
<cfinvoke component="cfcs.Hello"
    <cfinvokeargument name="nom" value="Peter">
<cfoutput><p>2) #greeting#</p></cfoutput>
<cfset vars = StructNew() />
<cfset vars.nom = "Peter" />
<cfinvoke component="cfcs.Hello"
        returnVariable="greeting" />
<cfoutput><p>3) #greeting#</p></cfoutput>
<cfobject name="cfc" component="cfcs.Hello" />
<cfoutput><p>4) #cfc.sayHello("Peter")#</p></cfoutput>
    cfc = createObject("component", "cfcs.Hello");
    greeting = cfc.sayHello("Peter");
    writeOutput("<p>5) " & greeting & "</p>");
    greeting = createObject("component", "cfcs.Hello").sayHello("Peter");
    writeOutput("<p>6) " & greeting & "</p>");

Leave a Reply

Your email address will not be published.