What is ColdFusion

ColdFusion is a dynamic web application server and a Rapid Application Development (RAD) toolkit that runs on Java. This document gets you started on ColdFusion (2016 release). This document talks about various aspects of ColdFusion from installing to deploying an application on a server.

Installing ColdFusion

Download the 32-bit server or the 64-bit server, according to your system configuration. You can download a trial version from the download site.

While installing, you can see two installation options:

  1. Installing server configuration: The ColdFusion server configuration contains an embedded copy of Tomcat. To install the server configuration, follow the procedure in Installing the server configuration.
  2. Installing JEE configuration: Deploy ColdFusion as a Java application on a standards-based Java Enterprise Edition application server. To install the JEE configuration, follow the procedure in Installing the JEE configuration.

In addition, refer to the ColdFusion support matrix for server and OS-based configurations.

If you are migrating from an older version of ColdFusion to ColdFusion (2016 release), refer to the migration guide.

Starting the ColdFusion Administrator

After installing ColdFusion, log in to the ColdFusion administrator to verify a successful installation.

The ColdFusion Administrator is a centralized interface for configuring your ColdFusion server. You can configure settings for mail, database connections, debugging options, security, and so on.

The default location of the ColdFusion Administrator login page is http://servername:8500/CFIDE/administrator/index.cfm, where servername is the fully qualified domain name of your host machine. Common values for servername are localhost or 127.0.0.1 (each refers to the web server on the local computer).

The default port number for the server configuration is 8500. If the port is occupied, then the next available port number is taken.

For more information, refer to Configuring and Administering ColdFusion.

ColdFusion Builder

ColdFusion Builder is an IDE for CFML-based projects or applications using ColdFusion Enterprise or Standard server. Since ColdFusion Builder is based on Eclipse, it works easily with other Eclipse plugins.

Like any IDE, ColdFusion Builder is equipped with a code editor, a compiler, a debugger, and code assist.

To know more about ColdFusion Builder, refer to Getting started with ColdFusion Builder.

ColdFusion basics

Syntax

You can write ColdFusion Markup Language (CFML) in two ways- tag-based and script-based. For example:

Tag-based Script-based

<cfset greeting="Hello World!">
<cfoutput>#greeting#</cfoutput>

<cfscript>
         greeting="Hello World!";

          WriteOutput(greeting);
</cfscript>
 

Variables

ColdFusion is a weakly typed language. The same variable can act as multiple data types. For example,

x=1;

The variable x can act like a numeric type, as shown below:

myNum=x^2;

The variable x can also act like a string type, as shown below:

myString=len(x);

In ColdFusion, there are two classes of variables by type:

  1. Simple: Simple variables include String, Numeric, Boolean, or DateTime.
  2. Complex: Complex variables include Structs, Arrays, Queries, or Images.

Strings

In ColdFusion, a string is a quote ( ' or " ) followed by zero or more letters, numbers, or symbols, and followed by another quote ( ' or " ). For example,

myString="Hello";

myString1="Welcome to ColdFusion.";

myString2="a123";

There are string functions in CFML for string manipulation and display. To see a list of all string functions, refer to String functions.

For example, use the len function as follows:

<cfscript>
       myString="That's one small step for man, one giant leap for mankind.";
       myStringLen=Len(myString);
       WriteOutput(myStringLen);
</cfscript>

The output is 58, which is the length of the input string including spaces.

Arrays

In ColdFusion, you can create arrays explicitly, by using a function to declare the array and then assigning it data, or implicitly by using an assignment statement. You can create simple or complex, multidimensional arrays.

You can declare arrays of strings, numbers, or objects. For example,

myArray=[1,2,3,4,5];

myArray1=["ab","bc","cd","de"];

To create an array, first create an array object using the ArrayNew function. For example,

<cfscript>

   myArray = [];

   myArray=[0,1,1,2,3,5,8,13,21];

</cfscript>

The example creates an array object for a one-dimensional array and then populates the array with the Fibonacci series.

For a complete list of array functions in CFML, see Array functions.

Lists

A list in ColdFusion is a string with delimiters. A delimiter separates each item in the list. For example,

myList="a,b,c,d,e";

To find the last element in a string, use the ListLast function, as shown below:

<cfscript>

       myList="Tokyo,Bangkok,Jakarta,Manila,Bangalore,Shanghai";

       WriteOutput(ListLast(myList)); //Outputs the last element in the list, Shanghai

</cfscript>

For all list functions in ColdFusion, see List functions.

Using member function, you can re-write the example above as shown below:

<cfscript>

        myList="Tokyo,Bangkok,Jakarta,Manila,Bangalore,Shanghai";

        WriteOutput(myList.listLast()); //Outputs the last element in the list, Shanghai

</cfscript>

For a list of member functions in ColdFusion, see Member functions.

Structs

A struct is a data structure that stores key-value pairs. It is similar to a HashMap in Java.

Create a struct object to hold the data. For example,

myStruct={};

To populate the struct with data, type myStruct.key1="value1";

For example,

<cfscript>

       myStruct={};
       myStruct.key1="John"; // Using dot notation
       myStruct["key2"]="Paul"; // Using associative array notation
       WriteOutput(myStruct.key1 & "<br/>"); // John
       WriteOutput(myStruct.key2); // Paul

</cfscript>

Using literal syntax,

<cfscript>

       myStructLiteral={};
       myStructLiteral={key1="one",key2="two"}; // Using literal syntax
       WriteOutput(mystructLiteral.key1 & "<br/>"); // one
       WriteOutput(mystructLiteral.key2); // two

</cfscript>

Loops in ColdFusion

You can iterate over a set of data and return values based on a condition. For example,

for (initialization;condition;iteration){

     // statements

}

For example,

<cfscript>

     for (i=1;i <=10; i++) {
     myResult=i^2;
     WriteOutput(myResult);
    }

</cfscript>

Pre-condition loops

Pre-condition loops evaluate a condition at the beginning of a statement, and continues to loop till the condition is true.

while (condition) {

     // statements

}

For example,

<cfscript>
       x = 0;
       while (x < 10) {
             x++;
             WriteOutput(x & " "); // 1 2 3 4 5 6 7 8 9 10
       }
</cfscript>

Post-condition loops

In this type of loop, the statements enclosed inside the do {} will execute irrespective of the condition.

do {

     // statements

} while (condition);

For example,

<cfscript>
       x = 0;
       do {
              x++;
               WriteOutput(x & " "); // 1 2 3 4 5 6 7 8 9 10
             } while (x < 10); 
</cfscript>

Array loops

You can iterate over each element in an array. For example,

for (element in [1,2,3,4,5]){

    WriteOutput(element); // Outputs 12345

}

Struct loops

You can iterate over each struct member and apply struct operations. For example,

myStruct={a=1,b=2,c=3,d=4,e=5};

for (key in myStruct){

     WriteOutput(#key# & ":" & #myStruct[key]#); // Outputs a:1 b:2…

}

List loops

You can iterate over each element in a list. For example,

myList="a,b,c,d,e";

for (element in myList){

     WriteOutput(element); // Outputs abcde

}

Data handling in ColdFusion

Handling record sets in databases is a key component of ColdFusion. ColdFusion has built in support for many databases including MS SQL, MySQL, Oracle, Derby, and Postgres.

You can use <cfquery> and call a SQL statement using a datasource. A datasource is the collection of settings you can use to communicate with a database, such as, database type, sever, port, tablespace, or database, username and password.

For example,

<cfquery name="myQuery" datasource="cfdocexamples">
       SELECT FIRSTNAME,LASTNAME 
       FROM EMPLOYEE
       WHERE SALARY>50000 AND CONTRACT='Y'
</cfquery>
<cfdump var="#myQuery#" >

You can also use QueryExecute within cfscript to query a table. For example,

<cfscript>

     myResult=QueryExecute("SELECT FIRSTNAME,LASTNAME FROM EMPLOYEES WHERE DEPARTMENT='Sales' AND LOCATION in (?,?)",["Newton","San Francisco"],

     {datasource="cfdocexamples"});

     WriteDump(myResult);

</cfscript>

For a list of all query functions, see Query functions.

Query loops

You can iterate through the contents of a recordset and display the contents. For example,

<cfscript>

     myQuery = queryNew("id,value", "integer,varchar",[

    [1, "East"],

    [2, "West"],

    [3, "North"],

    [4,"South"]

]);

 for (row in myQuery){
 WriteOutput(row.id & ":" & row.value);  // Outputs 1:East;2:West;3:North;4:South;

 }

</cfscript>

Application.cfc

Application cfc is used to define settings that are common to an application.  Application.cfc defines application level variables and event handlers (functions invoked at various application lifecycle events).

There are various lifecycle events in your ColdFusion applications. To know more about the events, refer to the Application.cfc reference.

For example,

component {

    this.name = "AppName";

    this.datasource = "AppDataSource";

    this.sessionManagement = true;

}

ColdFusion ORM

ColdFusion Object Relational Mapping (ORM) lets you access and manipulate databases using CFCs. ORM is built on Hibernate. Using ORM, you can build object oriented applications. You can concentrate on building the business layer without worrying about CRUD operations to the database. ORM generates SQL statements and defines the CRUD methods for you.

Update the Application.cfc as follows:

component{

     this.name="artgallery";

     this.ormEnabled=true;

     this.datasource="cfartgallery";

}

There is a list of ORM-related configuration for your CFC. Find it here.

Next, map an object to a database and define the persistence, as shown below:

component persistent="true"{

     property name="artistid" generator="increment";

     property firstname;

     property lastname;

}

Create a ColdFusion page to list all artists with first and last names, as shown below:

<cfscript>

     myArtists=EntityLoad("Artists");

     WriteDump(myArtists);

</cfscript>

For more details, see ORM in ColdFusion.

Object-oriented concepts in ColdFusion

Object-oriented programming in ColdFusion relies heavily on ColdFusion Components (CFCs). A component encapsulates data and the methods that can manipulate that data. Its parallel in java is the Java “Class”.

A CFC consists of:

  • Data/variables containing data.
  • Methods to manipulate that data.

Using a CFC, you can:

  • Share properties and variables between methods
  • Share properties between methods defined in other CFCs
  • Inherit methods and properties

Create a CFC (greetingsobjectFile.cfc) that displays the string "Hello World!":

component greetings {
       function helloWorld(){
             var myGreeting="Hello World!";
             return myGreeting;
       }
}

Create an object greet that calls the helloWorld method and displays the appropriate result.

<cfset greet=new greetings()>
<cfoutput>#greet.helloWorld()#</cfoutput>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy