Programming Languages
What is ColdFusion?
ColdFusion is a rapid application development system for professional developers who want to create dynamic Web applications and interactive Web sites. ColdFusion is an application server from Allaire. It is now more than 3 years old, so it is a bit more mature than many other application servers out there. Version 4 has just been released.
ColdFusion can be installed on Microsoft Windows NT or Sun Solaris, works with several web servers, and can access several different databases (including Oracle, Sybase, Microsoft SQL, Access). It provides the fastest way to integrate browser, server, and database technologies into powerful Web applications. With ColdFusion, you can build everything from online stores to sophisticated business systems.
Developing applications with ColdFusion does not require coding in a traditional programming language like Perl, C/C++, Visual Basic, Java, or Delphi. Instead, you build applications by combining standard HTML with a straightforward server-side markup language, the ColdFusion Markup Language (CFML).
Tips
Proper Comments
BenForta in his book Web Database Construction Kit tells us to use as the format to comment text. It's in the CF3.0 and CF3.1 online documentation too but actually It's three hyphen and not 2! Sounds pretty simple, but when you actually got used to using 2 and too lazy to add another hyphen the problem starts coming.
HTML comment tags for 2 hyphen and without space after and before the tags is fine. My experience with using 2 hyphen caused me to find the problem after several hours of testing the code and removing almost all the CFML tags to locate the culprit. I could not find the problem until I noticed I missed a space after the HTML tags and not on the CF tags I was commenting.
Crazy Color
When you use font colors on a CFML variable name, try to avoid using RGB coding in #RGBas in #FFFFFF for the color white instead use 'White' as in this example or remove the pound sign ) altogether.
#variablename#
Or
#variablename##variablename#
If you are rushing your project, that's good enough. If you'd rather stay with the convention, escape it with the of use double pound sign.
#variablename#
Loop and Populate
When you have a predefined number of groups in a database and you want to populate all the records segregated by groups you might think making different sqls will do the trick. What if you have 8 groups, then you will need to write 8 sqls! Image having 20.
The solution is to use CFLOOP and a dynamically written query name to be referenced in my selection box later. The code is written below.
name="ListPolicy#xNumber#" |
Take note of the following:
-The name of the query, ListPolicy#xNumber on the first pass of the loop ListPolicy#xNumber becomes ListPolicy1, then on the next sequence ListPolicy2 until the 6th loop, ListPolicy6.
-CFIF on xNumber, this will assign the first loop or the query ListPolicy1 to the group EP, the next sequence assigns ListPolicy2 to WH and so on.
How does my selection box looks like?
EP
WR
Default CFM?
Most web servers use default.htm or default.html or index.htm or index.html as the path default page. Now, what if you want a CFM file to be the default page but would not want to re-set the default file type setup?
Use JavaScript on the default page to open a CFM file
parent.location.href = "default.cfm"; |
Database Essentials
ColdFusion can be used to access data in virtually any database for which a drivers available and installed on the ColdFusion server. In all cases, ColdFusion primarily provides "wrappers" for database commands written in SQL. Therefore, knowledge of basic SQL programming will be necessary. In many cases, knowledge will be required also of the superset of SQL used with a particular brand of database engine. Oracle and MS Access are two of the database engines commonly used at NIEHS.
URL Encoded Format with no strings attached..
TheURLEncodedFormat() allows web developers to pass spaces and other non-alphanumeric string data in the URL of a page. Without this, only the valid characters would be picked up while the special characters (including spaces) would be dropped. What URLEncodedFormat does is replace the spaces with the"+" sign and the other special characters are replaced with the equivalent hexadecimal escape sequences.
Avoid Problems With Large Verity Collections
ColdFusion includes an implementation of 's popular Search '97 software, letting developers create and search collections of HTML files, most productivity-application documents (, PowerPoint, and so forth), , and even relational databases. Many Web builders, however, encounter problems trying to index large collections of documents with ColdFusion. Follow these three tips to help keep your Verity collections from causing problems:
- Keep your collections optimized. The ColdFusion administration interface has an option for optimizing your Verity collections. Use it often. If automation is your game, you can write and schedule a simple CFML template to do the optimizations for you.
- Consider numerous smaller collections. For example, if your site contains sections for marketing, sales, and products, consider creating a separate Verity collection for each section. Smaller collections are easier to manage, and you'll also be able to narrow searches to specific areas of your site. Your visitors will thank you.
- Increase memory. Nothing improves the performance of your Web applications more than additional server memory. With memory prices at an all-time low, it is a worthwhile investment.
If you still get errors indexing your files after optimizing your collections, splitting them into several sections, and adding memory to your server, consider purchasing a full-blown copy of Search '97 and creating your collections from that. ColdFusion can search external Verity collections created from the full product as well as from its bundled version of the search engine.
Deal With Undeliverable Email
SMTP mail generated by ColdFusion sits in a Spool directory on the server (usually \CFusion\Mail\Spoolon Windows NT) until it is sent. Mail that fails delivery because of problems connecting to the SMTP server gets converted into a text file and dumped into a directory called Undeliverable (\CFusion\Mail\Undeliverable on Windows NT). You have to manually intervene to resend messages returned to the undeliverable directory. This usually means moving the text files from the Undeliverable directory back to the Spool directory. ColdFusion regularly scans the Spooldirectory for messages to deliver, so the next time the program scans the directory it will attempt to resend the messages.
As you can imagine, checking the undeliverable directory on a regular basis is tedious at best. Luckily, there's a custom CFML tag that automates the task for you. CF_ResendUndeliverableEmail, available as freeware from the , automatically scans the Undeliverable directory for returned mail at scheduled intervals, and then moves any files that it finds to the Spool directory for delivery.
Create Static Pages
While dynamically generated ColdFusion pages have many advantages, they can create server overhead and lead to long download times for your users. Sometimes static HTML pages are good enough, and much faster and less resource-intensive--for example, if you have a series of reports that get generated only once a month, or a set of pages that rarely change.
Fortunately, it's easy to build static pages from CFML templates using the
and
First, you need to specify what CFML template to call in the
tag:
url="http://url.to.your.existing.CFM.template/"> |
After
executes the template, the output is stored in a variable calledCFHTTP.FileContent. The next step is to write the contents of CFHTTP.FileContentto an HTML file on the server:
file="YourPathandFileOnServer.html" |
This writes out the resulting HTML to the path and filename you specify in the file attribute.
Break the ODBC Lock
is one of the most commonly used databases serving as a back-end to ColdFusion applications. Although not particularly well suited to concurrent user applications, Access is a popular choice for development projects because it's cheap and easy to use.
A common scenario when using Access (and other desktop databases such as and ) involves the need to overwrite an existing Access database with an updated version. This is no problem, until you actually attempt to overwrite the Access database. In the default mode, when ColdFusion first accesses a database, it places a lock on the database file so that no other program will attempt to overwrite the database. You can, in the ColdFusion Administrator, configure ColdFusion to drop this lock after each database query. Unfortunately, depending on the ODBC driver, this may or may not work for your particular database. If that's the case, you'll need to find another way to break the ODBC lock when it's time to upload a new version of your database.
Inmost cases you can simply run a query against the database that causes an ODBC error. Create a CFML template that intentionally contains an invalid query (for example, try querying a field that doesn't exist in the database). The error causes ODBC to drop the connection to the database. The downside of this approach is that if someone hits a template that makes a valid call to the database before you upload your updated database, ODBC will try to lock the database that you're trying to overwrite. Making your updates during off-peak hours can minimize the chances of this happening.
A better method is to stop the ColdFusion Application Server and IDE services from the Control Panel (in Windows), upload the new database, and then restart the services. The whole process usually takes less than a minute and minimizes the chance of a bad upload. The downside is that your ColdFusion server must be down while the update is taking place. You can automate the entire process by writing batch file and then scheduling it through a scheduling tool. Again, doing this at off-peak hours is a good idea.
Dynamically Populate Java Applets
One of Cold Fusion's coolest features is its ability to coexist with other technologies, including Java. ColdFusion is an ideal way to populate Java applets with dynamically generated data.
A classic example consists of dynamically populating a Java ticker applet with stock data. The ticker applet, Pros roll 2.3, is available as freeware. Stock data is obtained by a custom CFML tag called CF_StockGrabber. CF_Stockgrabber is also freeware and is available for download from the . The process works by creating a query and then inserting the results of that query into the applet, like so:
Ticker Symbols="CNET,YHOO,MSFT" |
A working demo including source code is available on the author's .
Use ColdFusion as a Scripting Language
No one ever said that ColdFusion is only for building database-driven applications. In fact, Cold Fusion's tag-based syntax and built-in functions make CFML a perfect language for small scripting jobs. These two examples demonstrate how to use ColdFusion as a scripting language.
To output the current date, merely stick the following CFML code in your page template where you want the date to appear:
Today's date is #DateFormat(Now(),'mmmm dd, yyyy')# |
Likewise, you can use CFML to email the values submitted by an HTML form to a list of recipients. Put the following HTML code in the form's page:
Place this CFML code in a separate file called sendmail.cfm:
TO="recipient1@foo.com,recipient2@foo.com" |
The following information was submitted by the form:
=======================================
Name: #Name#
Email: #Email#
You might want to add some HTML to this sendmail.cfm page so that the user sees an acknowledgment or some other feedback that the message has been sent.
JavaScript vs. ColdFusion Validation
You can validate data entered by a user on the client side or the server side. Since ColdFusion is a server-side tool, it excels at server-side input validation. But there are times when client-side validation is a better way to go. Client-side validation doesn't force users to wait while the data is sent to the server, validated, and any error messages returned.
For client-side validation in ColdFusion, you have to use a scripting language such as JavaScript. Unfortunately, not all browsers support JavaScript, and many people with JavaScript-capable browsers deliberately disable that functionality for security or privacy reasons.
One solution is to use both client-side and server-side validation in your applications. It takes a bit of extra work to combine JavaScript and ColdFusion validation routines, but it can be worth it.
For example, suppose you have an HTML form that asks users to input their names and their email addresses. You want to require that the user enter something for both the Name and Email fields.
To ensure that forms submitted by users without JavaScript are properly validated with minimum delay, you might use both client- and server-side scripts:
Name: |
It's a bit of extra coding, and it could mean validating the data twice--once by the JavaScript on the client side and again by ColdFusion on the server side. But it makes your pages more responsive for users with JavaScript browsers while still validating data for users with non-JavaScript browsers.
Identify Variable Scope
ColdFusion applications use a number of different variable types, or scopes. These scopes let ColdFusion identify the source of the variable.
ColdFusion variable scopes include:
Variabletype: | Scopesyntax: | |
Query |
QueryName.variablename | |
URLparameter | URL.variablename | |
Formfield | Form.variablename | |
CFSET | Variable.variablename | |
HTTPcookie | Cookie.variablename | |
CGIenvironment | CGI.variablename | |
Client | Client.variablename | |
Server | Server.variablename | |
Customtag (CFML) | Attribute.variablename |
Avoid Problems With Large Verity Collections
ColdFusion includes an implementation of 's popular Search '97 software, letting developers create and search collections of HTML files, most productivity-application documents (, PowerPoint, and so forth), , and even relational databases. Many Web builders, however, encounter problems trying to index large collections of documents with ColdFusion. Follow these three tips to help keep your Verity collections from causing problems:
- Keep your collections optimized. The ColdFusion administration interface has an option for optimizing your Verity collections. Use it often. If automation is your game, you can write and schedule a simple CFML template to do the optimizations for you.
- Consider numerous smaller collections. For example, if your site contains sections for marketing, sales, and products, consider creating a separate Verity collection for each section. Smaller collections are easier to manage, and you'll also be able to narrow searches to specific areas of your site. Your visitors will thank you.
- Increase memory. Nothing improves the performance of your Web applications more than additional server memory. With memory prices at an all-time low, it is a worthwhile investment.
If you still get errors indexing your files after optimizing your collections, splitting them into several sections, and adding memory to your server, consider purchasing a full-blown copy of Search '97 and creating your collections from that. ColdFusion can search external Verity collections created from the full product as well as from its bundled version of the search engine.
Alternate Table Row Colors
Tables with lots of rows can be hard to read, as the rows can seem to run together. One solution is to alternate the color of each row. On the client side, you might use to alternate cell background colors. On the server side, you can use Cold Fusion's Current Row variable and MOD function to determine which row of the query you are currently printing to your HTML page.
SELECT * FROM CustomerRecords
|
|
|||