Using proven methods, Charles Mohnike provides you with an understanding of the ColdFusion Server and guides you through the use of ColdFusion Studio, enabling you to create your own ColdFusion applications quickly and easily.
Chapter 1: Introducing ColdFusion
Let's begin with a basic fact that will put your upcoming 21-day journey
into perspective, and maybe even make it less wearing on your psyche:
ColdFusion is all about saving time.
Like other Web development and application-server tools, it was developed by
people like you and me who grew tired of living in their computer chairs, folks
who wanted to get their projects finished and get out to see the world.
The 21 days you now begin to invest will pay off later in the form of time spent doing things other than hard-coding HTML documents and formatting boring text passages. How you spend that free time is entirely up to you, and way beyond the scope of this book.
In Day 1, you'll:
Get your first look at Allaire's ColdFusion.
Learn how it can make you the talk of your computing buddies and find
out whether it's the right tool for your project.
Cover the basic theory behind creating dynamic pages.
Look briefly at the client/server relationship that makes all this
high-tech wizardry possible.
What Is ColdFusion?
A couple computer types, brothers Jeremy and J.J. Allaire, created ColdFusion
in 1995. Jeremy, the business-minded one, had a print publication he needed to
regularly post to the Web, so he approached the coding brother, J.J, and asked
him to help build a simple application that would speed the task. When the
project was complete, both realized they had a hot property on their hands and
Allaire Corporation was born. Today, ColdFusion is in its fourth major release
and both brothers wear fine suits and drive fancy cars.
The official description of ColdFusion calls it a Web application
server, but depending on how you choose to use it, it can also be considered
a page-development tool, a database server, or your ticket to the high life. At
the heart of the program is a database-to-Web gateway. It allows you to
take an existing database file and serve it up via the Web, record-by-record,
without having to create new HTML files for each record.
Exploring a Sample ColdFusion Application
To clarify this concept, let's use an example. Using a common database program, Microsoft Access, I've been maintaining a database of Elizabeth Taylor's husbands, as illustrated in Figure 1.1.
Figure 1.1. A sample database table in Microsoft Access containing data on the men in Liz Taylor's life.
New Term -- A field is an element in a
database that categorizes information by type. If I create a database containing
names, occupations, eye color, hair color, and net worth, I'd define each
of these types as a field.
Now assume that I've decided it's vital that the world has this
information readily available. The traditional way to get this stuff on the Web
would be to sit down, fire up the text editor, and start coding HTML documents,
seven in all or eight if you count the fact that she married Richard Burton
twice.
But, hey, I know that creating seven or eight Web pages with the same type of
information on each is a waste of my time. Instead I'll use ColdFusion to create the pages dynamically.
New Term -- Generating a page dynamically
means to create it on-the-fly, or at the moment it is requested by the
user's browser. The opposite of a dynamic page is a static page, or
a plain old HTML document that physically resides on a server.
I upload the database to my ColdFusion-compatible Web service provider,
set it up as a datasource (don't worry, I'll get to this in Day 3, "Setting Up ColdFusion and Defining a Datasource"), and then create a single template file to serve up the information.
New Term -- A ColdFusion template file is
a Web page that contains text and code directing ColdFusion to perform an action
or actions. Templates use the file extension .CFM.
When someone accesses my template file from the Web and requests the record
for the little-known Larry Fortensky, it looks like Figure 1.2.
Figure 1.2. ColdFusion output for the database record containing "Larry
Fortensky".
Looks a lot like a regular Web page, right? That's an important point to
catch -- ColdFusion is transparent to the end user, save for the file
extension .CFM instead of the standard .HTML. Because
it's a back-end application, the average surfer won't even know
you're using it. He'll think you spent hours building pages just for
his benefit, and that's not necessarily a bad thing.
New Term -- A back-end application is one
that runs behind the scenes and is transparent to the Web surfer. Various
back-end apps are used to serve Web content, to gather data on site visitors,
and to control advanced features such as chat rooms, message boards, and so
on.
Why Should I Use ColdFusion?
Hopefully, at this point, light bulbs are popping over your head and bells are
ringing in your ears with reasons to use this great tool on your own projects.
Just in case there aren't, in the next few sections I'll cover a few
reasons why ColdFusion is a tool you will want to add to your repertoire of
development applications.
Database-Driven Sites
ColdFusion gives you the ability to control some of or all of your entire Web
site's content from a database. The beauty of this may not strike you just
yet, but consider a situation where you might be working with others who
aren't as HTML-savvy as yourself. Without ColdFusion, a change in content
means that someone has to come in and code a new HTML page, and that someone
will probably be you, regardless of the importance of whatever you were doing
when your pager went off. With ColdFusion, Web content can be changed or updated
by anyone who knows his way around an input form and an enter key. This means
that anyone in the company -- even those without database experience -- can
update their Web projects without you having to drive down to the office and
possibly miss the bonus round on "The Price Is Right."
In addition, database-driven sites give you the ability to present lots and
lots of material on the Web, and you only need to design a single template file.
Take my Liz database, for example. It's pretty small now, but someday if
that database should stretch into hundreds or thousands of husbands, my initial
template page would easily handle all of them. I'd have one killer Web site
that looked as if I'd spent hours designing pages.
Database-driven sites also present interesting opportunities to keep tabs on
your visitors. If your site required users to register with a login name and
password, for example, you could then use the database behind the scenes to
track their interests by logging the pages they visit. The concepts behind this
are covered further in Day 7, "Changing the Contents of a Database with
ColdFusion."
Ready for Business
Many businesses love databases, and these businesses may come to love
ColdFusion. It's not hard to see why. Many businesses maintain a database
of their products or services, and for an existing database, ColdFusion makes it
a short hop to a full-featured Web site. Show me a business with a large product
catalog and I'll show you, the Web developer, a quick way to deliver a
full-featured Web site that will have your client dancing in the streets.
Real-Time Web Presentation
Many businesses can use ColdFusion directly with their active databases. This
means that the moment someone in the warehouse records a product as shipped, the
pages on the Web site immediately reflect the change in inventory. In most
cases, businesses can continue to use the same database, spreadsheet, or word
processing software they've always used in the same way they've always
used it. Just imagine the reception when you march into a business and tell its
owners that you can put their entire operation online in real-time -- without
their forking over a cent to retrain employees.
Interfacing With Intranets
Due to the nature of their product or service, some businesses don't
really require an extensive Web presence, but this doesn't mean that you,
the ColdFusion developer, need to pass them by. Lots of businesses use
ColdFusion as an intranet tool.
New Term -- An intranet is an internal
network that is usually not available to Web users. Intranets are used within
businesses for employee tasks such as information sharing, maintaining the
company database, or vital communications such as "Hey, Jim in accounting,
how was the big bash last night?"
Many businesses have found that using Web-like interfaces to control their
intranets helps new employees assimilate quickly and easily. Many have at least
experimented with a Web browser, so by presenting intranet data in the form of
Web pages, businesses can avoid having to acquaint new hires with several
applications.
ColdFusion is ideal for building these Web-based interfaces, and its database
functions are just as valuable on intranets as they are on the Web outside.
Business Security
Site security is generally a pretty big concern for businesses, whether
it's keeping the advertising staff from rewriting editorial copy or keeping
outsiders from rifling through the company secrets. ColdFusion has several
security measures built in to the program, meaning that even inexperienced CF
designers don't need to worry much about leaving their sites open to
hackers and ne'er-do-wells.
E-Commerce
ColdFusion is e-commerce ready, with lots of built-in tools and tricks that
make it easy to build sites offering secure online transactions. I'll cover
this in depth in Day 20, "Using ColdFusion for E-Commerce
Applications," but for now just let the concept sink in. We have a tool
here that allows you to quickly serve up a huge database of products or
services, plus the ability to easily make them available for sale online. Bells
ringing yet?
Data Collection
It's not just about business, either. ColdFusion is also used by
government bureaucrats to get their reams of data on the Web. It is used by
community organizations that maintain lists of other community organizations,
and by common folk like you and me who just want to share our database of Boxcar
Willie's bootleg recordings with the world at large.
Neither is ColdFusion just about outputting documents to the Web. It features
a handy tool called CFINSERT, which I'll cover in Day 7. This feature alone
opens up whole new worlds of uses because it allows you to collect user input
from the Web, via a standard HTML form or other methods, and
automatically input it into a database on your server.
New Term -- A form is a Web page designed
to take input from the user. When you type a keyword into the text box on a
search engine and hit "submit," you're using a form.
Let's say that you're with one of the community organizations mentioned
previously, and you want to assemble a database of support groups for average
Joes who confess an addiction to watching "COPS." Using ColdFusion,
you'd post an input form on the Web with fields for "name of group,"
"meeting place," "time," and so on, as shown in Figure 1.3.
Figure 1.3. A Web input page designed with ColdFusion behind the scenes.
Each time a user fills out and submits the form, you have a new record in
your database. And then it's up to you what to do with the data. You could
serve it up on the Web as mentioned previously, or you could create a snail-mail
list to send out form letters to all those "COPS" Watchers Anonymous
groups around the globe.
In the same way, ColdFusion can be a huge asset to those who've embarked
on a personal quest to share their interests with the world at large. For
example, I'm quite the fan of '50s mambo music, but due to financial
constraints (and time spent writing this book) I don't have the time on my
hands to code Web pages for each of the hundreds of mambo LPs I own. If not for
time-saving programs like ColdFusion, the world might never get to share my
extensive knowledge of the mambo and would be all the poorer for it.
Better Than CGI
For the uninitiated, CGI scripts are tools Web developers use for handling
stuff like input data from forms, email, and site-specific searches. These
scripts are essentially small programs written in languages like Perl and C that
run on the Web server and vastly extend the capabilities of an HTML page by
allowing the server to actually do stuff other than just blindly serve up
Web pages.
Output -- CGI stands for Common Gateway
Interface, a set of commands understood by all standard Web server
packages.
ColdFusion does pretty much everything a CGI script will, so you needn't
quiver when I mention Perl and C.
If you're already familiar with common CGI tasks, you'll find
ColdFusion extends the capabilities of these and other functions. It offers
sophisticated mail and search handling (I'll cover these in Days 13,
"Using ColdFusion to Handle Email," and 17, "Finding Text with
Verity") without your having to work with scripting. It also handles stuff
like environment variables with elegance and can evaluate complex if-then
decisions with only a few lines of code.
New Term -- Environment variables are
variables used in CGI scripts to do things like determine the current time,
date, page hits, and so on.
If you know about programming languages, you'll be glad to know that
ColdFusion works seamlessly with your existing CGI scripts and you won't
have to replace all those scripts you slaved on last year.
ColdFusion is a little less risky than CGI when it comes to security as well.
If you've ever written a CGI application, you've probably dealt with
subroutines that handle malicious input from outside users. Even a simple CGI
mail handling script can leave potential security holes, and without special
precautions Jodie Foster may someday receive threatening mail traceable to your
client's server. ColdFusion builds security measures into the server, so
there's less worry.
Custom Applications
Advanced ColdFusion programmers have also used the tool to build a variety of
common Web applications such as chat rooms, message boards, and shopping cart
systems. These apps often have advantages over their standard CGI incarnations
because they allow use of a database behind the scenes. Chatters can have their
email logged to a database field, shopping carts can keep tabs on who's
buying what, and so on.
Many of those super-swift coders have made their pre-coded ColdFusion apps
publicly available, either free or for insane sums. An average ColdFusion user
can pop a pre-written app onto her Web site without having to reinvent the wheel
and thus make it appear to customers and clients that she's really on the
ball.
Tip -- Allaire lists hundreds of pre-coded
ColdFusion applications on its Web site,
http://www.allaire.com. A
quick browse through these makes a good starting point for new CF users.
You'll learn how other coders use ColdFusion and gain a better
understanding of what's possible with the program.
However, don't take this to mean that you can simply go out and spend a
few bucks for a pre-written ColdFusion app and skip the rest of this book.
You'll need a good grasp of ColdFusion theory to tailor the application to
your site's needs.
Dynamic Page Generation
ColdFusion's true finesse lies in its capability to produce
dynamic Web pages. As defined earlier in today's lesson, the term
"dynamic" is not used here as a measure of greatness (that's up
to your content), but describes the way a page gets from your server to the
user's screen. The opposite of a dynamic page is a static page, or a
plain old HTML document that lives on the Web server and gets served up when
someone enters a URL into his browser. Dynamic pages, by contrast, are created
on-the-fly by the server. They may only partially exist on the server until a
user requests them; then they're assembled and sent to the browser.
Take the previous example of Liz's husbands. The template file I created
to display each database record is not a complete HTML page. It may contain
elements of HTML such as headers and footers, but the actual data on Larry
Fortensky, his mullet, and his truck-driving career is missing. Larry's
data is added to the page only at the moment the user's browser makes a
request to the Web server. On the user's end, a dynamically generated page
looks identical to a static one.
Dynamically generated pages have other attributes as well. They may contain
code that makes decisions on-the-fly, such as deciding whether a user is surfing
with Netscape Communicator or Internet Explorer, and then delivering a page
optimized for that specific browser.
Let's look at a simple example of dynamic page generation in action. My Liz Web
site will have a constant header and footer throughout, as shown in
Figure 1.4.
Figure 1.4. One page from my sample Web site, showing the use of a header
above and a footer below.
New Term -- Header and footer are
terms used by Web designers that mean "stuff that appears at the top of the
page" (such as a navigation bar or icons) and "stuff that appears at
the bottom of the page" (such as site credits, disclaimers, and so on),
respectively.
The same header and footer appear with different body text in Figure 1.5.
Figure 1.5. Another page from my site, with the same header and footer
information from the one in Figure 1.4.
Note that both pages have the same text up top, as well as the same links at
the base of the page. Only the body text changes from page to page.
If you are new to this whole Web design thing, you might approach this the
hard way by manually typing in the code for each header and footer as you design
each individual page. If you've been around the Web scene for a while, you
might be a little wiser and save some time. You'd create a template page
with a header and footer, and then paste in the body text for each page, saving
the page as a new file each time.
But you're even smarter than that. Instead of duplicating the header and
footer information on every page in the site, you can generate pages dynamically
and let ColdFusion do the work. Using a standard text editor like Windows
Notepad, I'll create two simple files -- first, one called header.html.
<CENTER>
<FONT SIZE="+2">Charles' Useful Data That Absolutely Must Be
Shared With
the World</FONT>
<BR>
Here you'll find the answers to questions you've always
wondered
about but never had the guts to ask
</CENTER>
<HR>
In Listing 1.2, I'll create footer.html with the code necessary
to create links.
<HR>
<CENTER>
<A HREF="somelink.html">LIZ TAYLOR'S HUBBIES</A> |
<A HREF="somelink.html">BOXCAR WILLIE DISCOGRAPHY</A>
<BR>
<A HREF="somelink.html">MICHAEL BOLTON'S HAIRLINE</A> |
<A HREF="somelink.html">FAMOUS REST STOPS ON I-40</A>
</CENTER>
Finally, I can use the magic of dynamically generated pages to automatically
insert my header and footer into each page on the site at the moment the page is
requested by a user. I do it with a handy ColdFusion tag called
<CFINCLUDE>. As you'll learn in tomorrow's lesson,
ColdFusion tags work much the same way as HTML tags, but they vastly extend the
capabilities of regular old HTML.
Now I could add text like Listing 1.3 to each of the pages I create for my
site.
<HTML>
<BODY>
<CFINCLUDE template="header.html">
Some body text goes here
<CFINCLUDE template="footer.html">
</BODY>
</HTML>
When a user requests that page, ColdFusion quickly finds the files titled
header.html and footer.html and sticks them into the page. The
user sees only the dynamic output, as in Figure 1.4.
One of many advantages to doing things this way is that if I'd like to
change a link or a graphic later in the game, I only change it once in the
relevant header or footer file -- and it's immediately updated throughout
my site.
Dynamic pages can also be written to perform computations on variables, such
as totaling an order and applying sales tax. In short, they bring Web pages
alive, and once you begin using them you'll probably lie awake nights
dreaming up new uses for dynamic pages.
Understanding Clients and Servers
To really understand ColdFusion and how it serves up your pages, it's
important to grasp the basic concepts behind Web service. Let's start with
the server, which is any computer connected to the Internet. PC, Mac,
Silicon Graphics -- the make isn't important as long as it can connect to
the Net and has software running that allows it to serve up Web pages.
The server is -- in a perfect world -- always on and connected, just
waiting, hoping, pleading for a client machine to send it a request. The
client is the user's computer, the home or office machine on which
the user does his Web surfing. The client makes its requests in the form of
Uniform Resource Locators (URLs) that specify which server they want to connect
to, and what file they want to retrieve or execute. Without getting too
technical on the theories of packets and TCP/IP networking, lets just say that
once the client requests a URL, magic fairies carry it to the designated server.
A list of what servers ColdFusion will run on is covered more in depth in Day
3.
When the server receives a request from a client, it performs whatever
function the client has requested. Most often, the client has requested an HTML
page or an image file, so the server's task is simple -- send the
requested file back to the client. Pull out your highlighter now, because here
comes the key concept: Sometimes the client's request, though still a URL,
asks that the server perform an action, and then return the results of that
action to the user's browser. This is the basis of the CGI scripts
mentioned previously, and it is the key element that makes the wonder of
dynamically generated pages possible.
When a client requests a ColdFusion template or the name of a CGI script, the
server performs an action -- depending on the request, it might search a
database, run a computation on figures, or run a short program. It then returns
the output, or results of the action, to the client in the form of an
HTML document. The client doesn't care what goes on behind the scenes;
it's only concerned with the HTML output.
How ColdFusion Works Within the Client/Server
Relationship
In the client/server relationship, ColdFusion resides on the server computer,
running in conjunction with Web server software. It waits for a client to send a
request URL that has the .CFM extension designating a ColdFusion
template file, such as
http://www.blahblah.com/stuff.cfm
and then goes to work. It momentarily seizes control on the Web server, does
whatever is requested of it in the template file, and then gives the results
back to the Web server to hand over to the magic fairies for delivery to the
client.
Tag Processing
Open up any ColdFusion template in a text editor and you'll find a lot
of familiar HTML markup, including code such as <BODY>,
<TITLE>, and text formatting tags such as <I>
(italic) and <B> (bold). You'll also find some
not-so-familiar tags such as <CFQUERY>,
<CFOUTPUT>, and the aforementioned <CFINCLUDE>.
These are ColdFusion Markup Language (CFML). They're similar to HTML
tags except that they're recognized only by ColdFusion and require that
ColdFusion Application Server is running concurrently with your Web server
before they'll do anything other than place errors neatly up and down your
page.
Tip -- Most ColdFusion tags use opening and closing brackets just like HTML tags. For example, to designate a bold passage with HTML tags, you'd use a <B> before the relevant text and a </B&lgt to stop the bolding. ColdFusion uses the same characters to denote a beginning and ending tag, such as <CFQUERY> and </CFQUERY>.
When a ColdFusion server processes a template or .CFM file, it first
looks for CFML tags. These may tell ColdFusion to search a database, to output
some information, or to include a file (as in the earlier example).
You'll find a complete listing of ColdFusion 4.5 tags in Appendix B of
this book, and I'll discuss most of them in depth in lessons to come.
Database Connectivity
Once a ColdFusion server examines the tags on a page, it begins executing the
commands specified within the CFML tags.
One of the most common requests is a database search. To search efficiently,
ColdFusion needs to have the database ready for access when requests come in. If
each time the program received a request it had to open the database file,
search the records, and then close the file, it would be hopelessly inefficient
and unable to deal with multiple requests pouring in simultaneously.
ColdFusion works this out by requiring you to set up your database as an Open
Database Connectivity (ODBC) source. An ODBC source sets up a data pipeline in
and out of the database, making it readily accessible to ColdFusion.
Once a database is connected to ColdFusion via ODBC, developers can use a
special set of commands, called structured query language, or SQL, to
communicate with the database. SQL commands can be used to search a database,
change the information within, or even to create new databases.
You'll look at SQL, datasources (DSNs), and ODBC in Days 3 through 5.
For today, just marvel at the acronyms and sleep tight knowing you've
covered the basic concepts.
Summary
Two guys, who wanted to save time on their Web coding, so they could do other
stuff, created ColdFusion. By mastering the software and using it in your own
Web projects, you too can save many hours of coding and remain free to venture
into the daylight.
At the heart of the program is a database-to-Web gateway, a means of sharing
database information with Web users. ColdFusion can take databases containing
thousands of records and serve them up as individual Web pages via a single
template file. It also handles input, allowing Web users to contribute to a
database.
ColdFusion server is an ideal tool for business and organizations because it
allows the creation of very large sites with a minimum design time. It features
built-in security and commerce tools for those wanting to serve data
selectively, or those who offer products or services for sale online.
Q&A
Where can I get more information on ColdFusion before shelling out
the dollars for it?
Allaire's Web site at
http://www.allaire.com/ has
extensive information on Web software compatible with ColdFusion, and a 30-day
demo available for download. You might also try reading the rest of this
book.
Does ColdFusion work with all database types?
Yes, pretty much. But knowing that there will be someone out
there running some arcane office suite, I'd recommend checking the Web site
for a current list of supported file formats. I'll cover databases in depth
in Day 4, "Building a Database and Organizing Data," and Day 5,
"Finding the Data You Need: An Introduction to Queries and
SQL."
What about scalability? I have a huge product database and a Web
site that gets thousands of hits per
day.
Scalability is one of ColdFusion's
biggest selling points. Right out of the box, it can handle everything from the
simplest fan site to a huge database on a busy server. As you'll learn in
Day 3, it also includes options to tailor its own performance to the
capabilities of your server.
Workshop
The workshop provides quiz and exercise questions to test your understanding
of today's material. Answers to quiz questions can be found in Appendix A
at the back of the text.
Quiz
What is ColdFusion's primary function? Name two good uses for the
program.
How does ColdFusion work with CGI scripts?
What is dynamic page generation?
How does a ColdFusion-driven Web page get from a server to a
client?
Who was Elizabeth Taylor's most recent
husband?
Exercises
Check out Allaire's Web site and browse the Frequently Asked
Questions (FAQ) section on ColdFusion 4.
Also on Allaire's site, check out some of the links to other ColdFusion-driven pages. Note some of the ways others use ColdFusion in their design and compare them to your own design needs.
(c) Copyright Pearson Education. All rights reserved.
Charles Mohnike is a technology writer and the co-founder of Zapt Digital Media, an electronic publishing firm.
This article is brought to you by Sams Publishing, publisher of Sams' Teach Yourself ColdFusion in 21 Days.