CPCUG Monitor
Creating dynamic websites with ColdFusion
by Michael Smith
What is ColdFusion
In this article we explore what ColdFusion is and what it
can do for dynamic website creation. We also cover where you can learn more
about ColdFusion, including the free CPCUG ColdFusion Conference in June.
ColdFusion is a programming language based on standard HTML
(Hyper Text Meta Language) that is used to write dynamic webpages. It lets you
create pages on the fly that differ depending on user input, database lookups,
time of day or what ever other criteria you dream up! ColdFusion pages consist
of standard HTML tags such as <FONT SIZE=”+2”> together with CFML
(ColdFusion Meta Language) tags such as <CFQUERY>, <CFIF> and
<CFLOOP>. ColdFusion was
introduced by Allaire in 1996 and is currently on version 4.0
Hello World Program in ColdFusion
When a user requests a ColdFusion page (usually a file with
extension .CFM) from your webserver the ColdFusion Server runs the page then
outputs a standard HTML page for your webserver to return to the user's
browser. In the simplest case where there are just HTML tags and no CFML tags
in the page, no processing is done, and the HTML just passes through unchanged.
So if you wanted to write a "Hello World" program in ColdFusion it
would simply consist of HTML:
<HTML><BODY>
Hello World
</BODY></HTML>
HelloWorld.cfm
If there are CFML tags then ColdFusion will process them,
calculating any variables, if statements, and loops and return the dynamically
generated HTML. If you are familiar with C then you might compare ColdFusion to
a super C preprocessor for HTML!
Let's add some ColdFusion code to our Hello World example,
so that if will ask for the user's name and display Hello [your name] instead.
In standard HTML web pages you must get input such as the user's name is a
separate page from the page where you will process it. This is because once a
page is displayed on your browser the communication with the webserver has
ended - for the server to do further calculations requires a second page
request. (Note it is possible to run code on the browser, but this requires use
of JavaScript or other client-side addons to HTML that we won't get into here.)
Note: The fact that web pages stand alone, separated from
the server, is one of the more confusing points of writing web applications. It
is sometime described as stateless
and each page doesn't automatically retain any memory or state from preceding
pages unless your code explicitly passes the information around. Compare this
situation to a traditional program in Basic or C, where you can easily pass
global variables or parameters around different screens in your program.
So we add a new page GetName.cfm to get the user's name and
change the display page to HelloWorld2.cfm as below.
<HTML><BODY>
<FORM NAME="GetName"
ACTION="HelloWorld2.cfm" METHOD="post">
<INPUT TYPE="TEXT"
NAME="FirstName">
<INPUT TYPE="SUBMIT" VALUE="OK">
</FORM>
</BODY></HTML>
GetName.cfm
<HTML><BODY>
<CFOUTPUT>
Hello #FirstName#
</CFOUTPUT>
</BODY></HTML>
HelloWorld2.cfm
The first page GetName.cfm is straight HTML for an input
screen. The sceond page HelloWorld2.cfm contains two ColdFusion constructs -
the <CFOUTPUT> tag to turn on ColdFusion variable output (with the
matching </CFOUTPUT> tag turning it off). And the pound symbol (#) to
delimit the variable FirstName that is passed as a form variable from the
GetName.cfm page. When the pages are run and if you type in the name Michael
into the first page then you will see "Hello Michael" on the page and
the following HTML will have been generated:
<HTML><BODY>
Hello Michael
</BODY></HTML>
Let's do one final thing with this example, and have it say
Good morning, afternoon or evening depending on the time of date of the server.
We will use the ColdFusion functions Now() and Hour() which return the current
date/time and the hour between 0 and 23 of a date/time value respectively.
<HTML><BODY>
<CFOUTPUT>
<CFIF Hour(Now())GT 18>
Good Evening
<CFELSEIF Hour(Now())GT 12>
Good Afternoon
<CFELSE>
Good Morning
</CFIF>
#FirstName#
</CFOUTPUT>
</BODY></HTML>
HelloWorld3.cfm
We have also used the <CFIF> <CFELSE>
</CFIF> tags which allow the page to display different results depending
on the hour of the day - whatever conditions you might prefer. Notice that
because we are using HTML, the carriage returns in the text don't matter, and
the greeting "Good Morning Michael" will appear on one line. If you
wanted them on two lines you would use the standard linebreak HTML tag
<BR> before the variable #FirstName#.
ColdFusion contains over a hundred functions for Arrays,
Date and Time, Decisions, Display and Formatting, Dynamic Evaluation, List
Processing, Structures, International, Mathematics, Strings, System values and
Query manipulation. There are about 70
tags for Database Manipulation, Data Output, Variable Manipulation,
Flow-Control, Internet Protocols, File Management, Web Application Framework,
ColdFusion Forms, External System Tags. Additionally you can write your own new
tags in either ColdFusion or C.
How ColdFusion Works
To summarize, a ColdFusion application is very simply a
collection of pages, similar to a static Web site. But unlike the pages in a
static Web site, the pages in a ColdFusion application include the server-side
ColdFusion Markup Language (CFML) in addition to HTML. CFML gives you the
ability to control the behavior of your applications, integrate a wide range of
server technologies, and dynamically generate the content that is returned to
the Web browser.
When a browser requests a page in a ColdFusion
application, it is automatically pre-processed by the ColdFusion Application
Server. Based on the CFML in the page, the Application Server executes the
application logic, interacts with other server technologies, and then
dynamically generates an HTML page, which is returned to the browser.
The diagram below shows what happens when a Web browser
requests a page in a ColdFusion application.
1.When a user requests a page in a ColdFusion application
by submitting a form or clicking a hyperlink, the user’s Web browser sends an
HTTP request to the Web server via the Internet or Intranet.
2.The Web server passes the data submitted by the client
and the requested page to the ColdFusion Application Server either through a
server API or CGI. ColdFusion pages are automatically compiled and cached in
memory so processing in is very fast and scaleable even under high loads.
3.ColdFusion reads the data from the client and processes
the CFML used in the page. Based on the CFML the ColdFusion Application Server
executes the application logic and interacts with a wide range of server
technologies includign database, email and files.
4.ColdFusion dynamically generates an HTML page and
returns it to the Web server.
5.The Web server then passes the page back to the user’s
Web browser.
What else can I do with ColdFusion?
If all you could do with ColdFusion was variables and CFIF
conditionally output web pages then it would be pretty lame. But you can do
many other things including:
·
Retrieve data from any ODBC database including Access
and SQL server
·
Run any SQL query including INSERT, UPDATE and DELETE
queries
·
Send customized email with CFMAIL
·
Loop over database queries, list or do For Next loops
·
Handle errors and relocate to different pages
·
Automatically read pages from other websites using
CFHTTP
To Learn More
If you are interesting in learning more about ColdFusion
CPCUG and TeraTech are holding a free ColdFusion User Conference on Saturday
6/26/99 at the Masur Auditorium from 9am ot 6pm. The morning will contain
introductory sessions and the afternoon advanced ones. You can sign up for the
conference at http://www.teratech.com/cfconf/
or call 301-424-3903.
You can download a free 30day evaluation version of
ColdFusion from Allaire or request a free eval CD-ROM from the Allaire website http://www.allaire.com/
Allaire Corporation
1 Alewife Center
Cambridge, MA 02140
Tel: 617.761.2000 voice
Fax: 617.761.2001 fax
Toll Free: 888.939.2545
Email: [email protected]
Web: www.allaire.com
CPCUG Discount
ColdFusion costs $1295 direct from Allaire. TeraTech is
offering a 10% discount for all CPCUG members and their organizations. Call
301-424-3903 for this
ColdFusion Resources
Allaire also maintain an extensive knowledge basis and
tech support forums on their website.
CPCUG and TeraTech ColdFusion Conference http://www.cfconf.org/
TeraTech maintains a ColdFusion code cuttings called
ColdCuts at http://www.teratech.com/ColdCuts/.
This page also has links to about a dozen ColdFusion white papers in the CF
Info Center.
The Maryland ColdFusion User Group meets the second
Tuesday of each month at Backstreets Cafe, 12352 Wilkins Avenue, Rockville. See http://www.cfug-md.org/ for details and
directions.
The DC ColdFusion User Group meets the first Wednesday
each month at Figleaf , 16th and P St NW, Washington DC. See the
DCCFUG page on http://www.figleaf.com/
for details and directions.
Bio
Michael Smith is president of TeraTech, a ten year old
Rockville Maryland based consulting company that specializes in ColdFusion,
Database and Visual Basic development. You can reach Michael at [email protected] or 301-424-3903.