Coldfusion temperature conversion web service

Posted on : 26-09-2009 | By : admin | In : Coldfusion

0

tempTime for a little example of Coldfusion web services! You heard me right, we are going to delve into a short and sweet example of how to create, and invoke a web service using Coldfusion!

It’s a very simple example of converting a temperature value from Farenheit to Celsius or vice versa. This will all be done by creating a form to accept user input, and a web service containing the logic to perform the conversion operations. Let’s hit it!

Our example will consist of three files. convTempForm which is the web form to accept user input, convTemp to process the input, call the web service, and display the results, and finally the web service Coldfusion component itself. Easy cheesy!

First let’s take a look at our user form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Temperature Conversion Web Service</title>
<style>
form{background-color:#99CC66; padding:10px;}
input, select{display:block;margin-bottom:5px;}
</style>
</head>
 
<body>
<form action="convTempForm.cfm" method="post">
    Provide a temperature:
    	<input name="temperature" type="text"/>
    Choose a Conversion:
        <select name="conversionType">
            <option value="CtoF">Celsius to Farenheit</option>
            <option value="FtoC">Farenheit to Celsius</option>
        </select>
    <input name="submitform" type="submit" value="Submit"/>
</form>
</body>
</html>

Nothing out of the ordinary here. We have a basic form that accepts a text value named temperature, and a conversion selection of either F to C, or C to F. Then a submit button posts the input to the action page.

Here is our action page:

<cfif #form.conversionType# is "CtoF">
	<cfscript>
		convertTemp = CreateObject("webservice", "http://localhost:8500/tempConversion/convComponent.cfc?wsdl");
		newTemp = convertTemp.ctof("#form.temperature#");
		inxml = getSOAPRequest(convertTemp);
		outxml = getSOAPResponse(convertTemp);
	</cfscript>
    <cfoutput>
    	#form.temperature# &deg;C is #Round(newTemp)# &deg;F.
	</cfoutput>
<cfelseif #form.conversionType# is "FtoC">
	<cfscript>
		convertTemp = CreateObject("webservice", "http://localhost:8500/tempConversion/convComponent.cfc?wsdl");
		newTemp = convertTemp.ftoc("#form.temperature#");
		inxml = getSOAPRequest(convertTemp);
		outxml = getSOAPResponse(convertTemp);
	</cfscript>
	<cfoutput>
        #form.temperature# &deg;F is #Round(newTemp)# &deg;C.
    </cfoutput>
</cfif>

This is our coldfusion form processing page that receives the posted data, and calls the web service component to perform the calculations. The way it gathers the data from the form is by using the variable scope ‘form’.

#form.conversionType#

There are two scenarios presented here. One is if the user chose CtoF from the select form element, and another if the user chose FtoC. Both scenarios call the same web service component, but separate methods. We use a coldfusion if/else statement to determine which method to target.

If the user wants to convert from Celsius to Farenheit, the if part of our if/else statement will trigger, and we create a web service object and target the ‘ctof’ method. If the user wants to convert from Farenheit to Celsius then the else part of our if/else statement is triggered and we create a web service object and target the ‘ftoc’ method.

Let’s get granular on this puppy. If our user provides a temperature of 45 degrees, and chooses to convert it from celsius to farhenheit, that data is posted to the action page of our form and is used to determine the if/else statement. In this case the if statement is triggered because the #form.conversionType# is ‘CtoF’. So our the first part of our if statement is true

<cfif #form.conversionType# is "CtoF">

Because it is true the logic included in the if statement will no run

	<cfscript>
		convertTemp = CreateObject("webservice", "http://localhost:8500/tempConversion/convComponent.cfc?wsdl");
		newTemp = convertTemp.ctof("#form.temperature#");
	</cfscript>

What we have here is a cfscript tag with several variable declarations. First we create an instance of our web service…

convertTemp = CreateObject("webservice", "http://localhost:8500/tempConversion/convComponent.cfc?wsdl");

This instance of the web service is stored in the variable convertTemp. The CreateObject function is one that creates an object of the assigned type (in this case ‘web service’), and the absolute URL to the WSDL that describes the web service.

Next we assign a variable to hold the result of the targeted web service function…

newTemp = convertTemp.ctof("#form.temperature#");

Using dot syntax we scope the method that exists within the instance of the web service object, and we pass to it the value that our user supplied in the form. Before we look at the result of the method logic, let’s look at the actual web service component that will perform the logic.

<cfcomponent>    
    <cffunction name="ctof" access="remote" returntype="numeric" output="no">
    <!--- Celsius to Fahrenheit conversion method. --->
        <cfargument name="temp" required="yes" type="numeric">
        <cfreturn ((temp*9)/5)+32>
    </cffunction>
 
    <cffunction name="ftoc" access="remote" returntype="numeric" output="no">
    <!--- Fahrenheit to Celsius conversion method. --->
        <cfargument name="temp" required="yes" type="numeric">
        <cfreturn ((temp-32)*5/9)>
    </cffunction>
</cfcomponent>

Here we see the coldfusion component that performs the logical operations on our users input. Depending on their choice (’CtoF’ or ‘FtoC’ ) the associated method with the same name will run. The arguments you see in the component are required for the method to run, and the name of the argument, ‘temp’ is a variable that is passed the value provided by the user in the form ( #form.temperature# ). The type of data the argument will accept is set to numeric, so it must be a number.

<cfargument name="temp" required="yes" type="numeric">

Once the argument is passed the value, it can now be used in the calculation to be returned to the user.

<cfreturn ((temp*9)/5)+32>

The cfreturn tag will perform the calculation ( passed value multiplied by 9, then divided by 5, then summed with 32 ), and then return the result to the logic that created and called the web service.

Remember this line from our action page?

newTemp = convertTemp.ctof("#form.temperature#");

The variable ‘newTemp’ holds the result of the web service operation, and can now be displayed on our page.

<cfoutput>
#form.temperature# &deg;C is #Round(newTemp)# &deg;F.
</cfoutput>

The original form scoped temperature provided by our user is included in a string that includes the rounded return value of the function

Some key points:

ColdFusion scripting

<cfscript></cfscript>

allows you to write portions of your templates with script-based syntax, which is often more concise and straightforward than ColdFusion’s traditional tag-based syntax.

The Round() function rounds a number to the closest integer that is larger than the input parameter, and returns an integer.

Happy scripting!!

Write a comment