Working with ColdFusion Application Pages

While you can code your application pages using NotePad or any HTML editor, this book uses ColdFusion Studio because it provides many features that make ColdFusion development easier. You should install ColdFusion Studio if you have not done so already.

About applicaton pages

From a coding perspective, the major difference between a static HTML page and a ColdFusion application page is that ColdFusion pages contain ColdFusion Markup Language (CFML). CFML is a markup language that is very similar in syntax to HTML, so Web developers find it intuitive. Unlike HTML, which defines how things are displayed and formatted on the client, CFML identifies specific operations that are performed by ColdFusion Server.

Creating application pages

The following procedure creates a simple ColdFusion Application page, which you use for other examples in this chapter.

To create a ColdFusion application page:

  1. Open ColdFusion Studio.
  2. Select File > New and select the Default Template for your new page.
  3. Edit the file so that it appears as follows:
    <html>
    
    <head>
    
    <title>Call Department</title>
    
    </head>
    
    <body>
    
    <strong>Call Department</strong><br>
    
    <!--- Set all variables --->
    
    <cfset department="Sales">
    
    <!--- Display results --->
    
    <cfoutput>
    
    I'd like to talk to someone in #Department#.
    
    </cfoutput>
    
    </body>
    
    </html>
    
    

Saving application pages

Instead of saving pages with an htm or html file extension, you save ColdFusion application pages with a cfm or cfml extension. By default, the Web server knows to pass a page that contains a cfm extension to the ColdFusion Server when it is requested by a browser.

Save ColdFusion application pages underneath the Web root or another Web server mapping so that the Web server can publish these pages to the Internet. For example, you can create a directory myapps and save your practice pages there.

To save the page:

  1. Select File > Save.
  2. Save your page as calldept.cfm in myapps under the Web root directory.

    For example, the directory path on your machine might be:

    (on Windows NT) c:\inetpub\wwwroot\myapps

    (on UNIX) <mywebserverdocroot>/myapps

Viewing application pages

You view the application page on the Web server to ensure that the code is working as expected. Presently, your page is very simple. But, as you add more code, you will want to ensure that the page continues to work.

To view the page in a local browser:

  1. Open a Web browser on your local machine and enter the following URL:

    http://127.0.0.1/myapps/calldept.cfm

    where 127.0.0.1 refers to the localhost and is only valid when you are viewing pages locally.

  2. Use the Web browser facility that allows you to view a page's source code to examine the code that the browser uses for rendering.

    Note that only HTML and text is returned to the browser.

    Compare the code that was returned to the browser with what you originally created. Notice that the ColdFusion comments and CFML tags are processed, but do not appear in the HTML file that is returned to the browser.
    Original ColdFusion page
    HTML file returned by Web server
    
    <html> 
    
    <head> 
    
    <title>Call Department</title> 
    
    </head> 
    
    <body> 
    
    <strong>Call Department</strong><br> 
    
    <!--- Set all variables ---> 
    
    <cfset department="Sales"> 
    
    <!--- Display results ---> 
    
    <cfoutput> 
    
    I'd like to talk to someone in #Department#. 
    
    </cfoutput> 
    
    </body> 
    
    </html> 
    
    
    
    <html> 
    
    <head> 
    
    <title>Call Department</title> 
    
    </head> 
    
    <body> 
    
    <strong>Call Department</strong><br> 
    
     
    
     
    
     
    
     
    
    I'd like to talk to someone in Sales. 
    
     
    
    </body> 
    
    </html> 
    
    

Reviewing the code

The application page that you just created contains both HTML and CFML. You used the CFML tag cfset to define a variable, Department, and set its value to "Sales." You then used the CFML tag cfoutput to display text and the value of the variable. The following table describes the code and its function:
Code
Description

<!--- Set all variables ---> 

CFML comment, which is not returned in the HTML page.

<cfset Department="Sales"> 

Creates a variable named Department and sets the value equal to Sales.

<!--- Display results ---> 

CFML comment, which is not returned in the HTML page.

<cfoutput> 

I'd like to talk to someone in #Department#. 

</cfoutput> 

Displays whatever appears between the opening and closing cfoutput tags; in this example, the text "I'd like to talk to someone in" is followed by the value of the variable Department, which is "Sales."



Banner.Novgorod.Ru