February 27, 2021
Hot Topics:

Excerpt: Introducing ColdFusion

  • By Charles Mohnike
  • Send Email »
  • More Articles »

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.

  <FONT SIZE="+2">Charles' Useful Data That Absolutely Must Be  
  Shared With 
  the World</FONT>

  Here you'll find the answers to questions you've always 
  about but never had the guts to ask



In Listing 1.2, I'll create footer.html with the code necessary to create links.



  <A HREF="somelink.html">LIZ TAYLOR'S HUBBIES</A> |


  <A HREF="somelink.html">MICHAEL BOLTON'S HAIRLINE</A> |
  <A HREF="somelink.html">FAMOUS REST STOPS ON I-40</A>


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.



  <CFINCLUDE template="header.html">

  Some body text goes here

  <CFINCLUDE template="footer.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.

Page 2 of 3

This article was originally published on May 7, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date