Cold Fusion Tutorial

jellytrickInternet και Εφαρμογές Web

10 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

86 εμφανίσεις

Cold Fusion Tutorial

Chris Marin

Web Application Server

Cold Fusion is made by Allaire

Allaire and Macromedia are in the process of

We can expect even tighter integration of their tools

Macromedia’s Dreamweaver Ultradev already works
with Cold Fusion

Macromedia and Allaire have been working on a
project called Harpoon that integrates Cold Fusion
with Flash.

Request/Response Diagram

In a traditional web transaction a
client requests an HTML document
from the web server, which returns
it. In other words, a web server is
really only acting like a glorified file

When an application server like Cold
Fusion is added, requests for .cfm
documents are passed from the web
server to the application server to
be processed before being sent to
the client.

The client still only receives HTML.
As far as it is concerned a .cfm
document is no different than a
.htm document.

Client Running Browser
Web Server
Traditional Web
Client Running Browser
Web Server
Cold Fusion
N-tier Architecture/
Dynamic Sites
Comparison to other Server Side

Other popular technologies for server side web development include:

Microsoft’s Active Server Pages

Java Server Pages



Cold Fusion’s strength is that the tag based language that it relies upon called CFML is
extremely simple in comparison to the others.

Cold Fusion Markup Language was built from the ground up for web application
development. Some of the other technologies, like JSP and ASP, started with a general
purpose language, and then used somewhat awkward contortions to turn them into web
scripting languages. Perl on the other hand was invented before the advent of the World
Wide Web.

You can often do something in three or four lines with CF that take 10 or fifteen with the

One of the disadvantages of CF is that the Professional and Enterprise editions cost $1300
and $5000 respectively, whereas ASP comes with all Microsoft Servers and JSP/PHP/Perl
can be had for free (Cold Fusion Express is available for free for noncommercial use). Cold
Fusion is also not as fast as something like PHP.

Cold Fusion Markup Language Primer

Setting variables:


<cfset hello= “Hello World” > //Sets a variable called test to the string Hello


<cfset foo = 5> //Sets a variable called foo to the number 5. Note the lack of
quotation marks.


<cfset truth = true>//Sets the variable truth equal to the boolean value true.


<cfset testarray = ArrayNew(1)>//Creates a 1 dimensional array called testarray

<cfset testarray[1] = "Bob">//Inserts the string Bob into the first position

<cfset testarray[2] = "Billy">//Inserts the string Billy into the second position.

<cfset twodarray = ArrayNew(2)>//Creates a 2D array.

<cfset twodarray[1][1] = "Bob">

<cfset twodarray[1][2] = "Billy">

Cold Fusion Markup Language Primer

Evaluating Expressions:

Cold Fusion supports the usual Operands: +

* / MOD

<cfset bar = 5 + 5> //Sets the variable bar to the number 10.

To concatenate strings use &

<cfset hello= “Hello World” >

<cfset twohellos = hello & hello>//Sets variable twohellos equal to the string
Hello WorldHello World

If/Else Conditionals

<cfif foo is 5>

//Do something

<cfelseif foo is 4>

//Do something else


//If the previous conditions were not satisfied do this.


Cold Fusion Markup Language Primer


<cfloop from="1" to="5" index="i">

//Do something i number of times.


Include another page

<cfinclude template=“somepage.htm">//Works just like SSI

Output stuff back to the browser

<cfoutput >

#name_of_variable# //It is possible to intermix HTML/JavaScript here


Cold Fusion Markup Language Primer

Variable Scope

Local Variables

<cfset hello= “Hello World” > //Sets a local variable. This variable is only valid
on that page.

Session Variables

<cfset Session.hello= “Hello World” > //Sets a Session variable. This variable
can be accessed across multiple pages during a single visit.

In order to use Session variables you must create a document called
Application.cfm and insert the line: <CFAPPLICATION NAME=“App_Name"

URL Variables

Url.variable_name //URL variables. For example in the URL

The variable urlvar is set to test55.

Form Variables

Form.variable_name //Form variables are set in HTML form objects.

HTML Forms

The most important attributes are “action” which specifies the template you want to send
the values to and the name/value pairs of each form object.

Typical HTML Form:

<form action="outputform.cfm" method="post">

<p> Name:

<input type="text" name="name">


<p> Gender : Male

<input type="radio" name="gender" value="male">


<input type="radio" name="gender" value="female">



<select name="school">

<option value="Cal">Cal</option>

<option value="Stanford">Stanford</option>

<option value="UCLA">UCLA</option>



<input type="submit" name="Submit" value="Submit">


Use Form Variables

To output to the browser:


#Form.gender# <br> <br>


To email the form variables:

<cfmail to="" from=""

subject=" visited the site" server=""> is a #Form.gender# that goes to


SQL Primer/The Most Useful Commands


Allows you to pull records out of a database. Usage:

SELECT [Field names] FROM [Table Names] WHERE [Some Expression]

For example. To get the names and schools out of the user table we could use:

SELECT name, gender FROM user

To get all of Lisa’s info we could use:

SELECT * FROM user where name = ‘Lisa’ //Note the single quotes


Allows you to put records into a database. Usage:

INSERT INTO [Table Name] ([Name of field 1], [Name of Field 2],…)

VALUES ([Value 1], [Value 2],…)

For example, to put a new person in this table we could use:

INSERT INTO user (name, gender, school) VALUES (Bob, male, Cal) //Note that the
ID field in this particular table is generated automatically

SQL Primer/Using DB Queries with CF


Allows you to update database records. Usage:

UPDATE [Table Name] SET [Field Name] = ‘[Some Value]’

WHERE [Some Expression]

For example. To change Lisa’s school we could use:

UPDATE users SET school = ‘Cal’ WHERE ID = 2

To use SQL queries in CF:

<cfquery name=“query_name" datasource=“ds_name" dbtype="ODBC">



For example, to get values out of a form and put them in the db we could use the following:

<cfquery name="insertvalues" datasource="tutorial" dbtype="ODBC">

INSERT INTO user (name, gender, school)

VALUES('', '#Form.gender#', '')


Using DB Queries with CF

To get something out of a db to display in the browser

First Run the query:

<cfquery name="getstuff" datasource="tutorial" dbtype="ODBC">



Then output the results to the browser

<cfoutput query="getstuff">

#name# #school# #gender# <br>


Each field must be enclosed within # signs. CF will automatically loop through
all of the records returned by the query and print them. In this example, the
HTML tag <br>inserts a line break after each record.

We just scratched the surface…

Cold Fusion has a rich expression language. The easiest way to access
this is to use Cold Fusion Studio’s Expression Builder

CF gives you easy access to things like POP, LDAP, CORBA, COM/DCOM

You can instantiate Java Beans as Cold Fusion objects

You can use CF to write WAP applications

CF has a built in search engine. It can create an Index of db tables or
scan directories for MS Office/PDP/HTML documents. It also allows you
to use boolean searches and performs ranking.

It is possible to extend CF by writing your own tags in CFML/Visual
C++ or Java.

Visit Allaire’s Developers Exchange where you can download hundreds
of free tags.

The Future

Allaire also makes a JSP server called JRun

Jrun and Cold Fusion are set to merge in a product called
Neo by the end of this year.

Neo will be a J2EE compliant application server that will
automatically turn CFML templates into portable servlets.

Preliminary public demonstrations of Neo showed that it
ran three times as fast as CF does now.

With this setup we get the best of both worlds, the power
of J2EE combined with the simplicity of CFML (At least in
comparison to other languages, check out Perl