In this second part of the paper, I'll focus on the specifics of
generating WAP content with ColdFusion, including the features
available in CF and CF Studio/HomeSite for creating WAP code. I'll
conclude with several useful tips and traps gathered from the
experience of having developed CF/WAP applications.
Serving WAP Content via ColdFusion
The most important thing you need to know when trying to create
WAP applications in ColdFusion is how to tell the browser calling a
ColdFusion program that the code it will receive is the proper MIME
type expected by that browser.
The All Important MIME Type, Set with CFCONTENT
In wireless applications - specifically, in WML
applications - the MIME type should be text/vnd.wap.wml.
Normally, one sets up the web server to get .wml pages sent to the
browser with that MIME type.
When generating dynamic WML code with ColdFusion, however, the
file type used is not .wml but .cfm. This tells the web server to
pass the ColdFusion code to the CF server for processing. But
whereas ColdFusion is installed by default to create output with a
MIME type of text/html, we need specifically to send WML code with
the proper WML MIME type.
To do that, we use the ColdFusion <CFCONTENT> tag,
typically placed at the very top of the ColdFusion template that
will be generating WML content. Specifically, the tag will be:
<CFCONTENT TYPE="text/vnd.wap.wml">
If you have some ColdFusion experience, you may know that this
tag has been around for many releases - it's not a new
addition for generating WAP content. Its sole purpose, which
happens to be very useful here, is to change the MIME type of the
page being generated.
You can use a <CFCONTENT> tag in the same way for
templates that generate HDML or WMLScript, substituting the
appropriate MIME types.
To put this tag in its proper context, here's a skeletal code
template for creating a WML deck within CF. Notice that the only
ColdFusion aspect of it is the very first line, with the
<CFCONTENT> tag slipped in before the XML declaration:
<CFCONTENT TYPE="text/vnd.wap.wml"><?xml
version=1.0"?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"
"http://www.phone.com/dtd/wml11.dtd">
<wml>
<card>
</card>
</wml>
Because this is such a fundamental piece of code, necessary for
any CF/WML development, Allaire has altered the latest release of
ColdFusion Studio (the CF editing environment) - and its
cousin HomeSite - to be able to create this sort of thing
whenever you request to create a new template. I'll explain this in
more detail later on.
If you're an experienced WML developer, you'll have noticed that
this page is set up to use the Phone.com WML document type
definition (DTD). You can just as easily choose to type in the WAP
Forum's DTD declaration on the second line, if you prefer. (Later
on, I'll show how you can change Studio/HomeSite's new page Wizard
to do that as well.) One other minor point to note is that I've
placed the <CFCONTENT> on the same line as the XML
declaration, to avoid unnecessarily creating carriage returns
before that tag, which some wireless browsers complain about.
From here, you're free to insert any ColdFusion code you want,
either to create WML elements for sending to the user, or to
perform server processing on behalf of the user. A variation on the
classic "Hello World" example can be presented using simple
ColdFusion variable and output processing, as in the following:
<CFCONTENT TYPE="text/vnd.wap.wml"><?xml
version=1.0"?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"
"http://www.phone.com/dtd/wml11.dtd">
<wml>
<card>
<CFSET Name="Charlie">
<CFOUTPUT><p>Hello,
#name#!</p></CFOUTPUT>
</card>
</wml>
This is a very simplistic example, of course. There's nothing
terribly dynamic about it (the very first fragment you saw was more
realistic). But it does demonstrate the basics of creating and
using variables in CF. If you're new to WML, you should take note
that we surround any text sent for display to the wireless browser
in <p> tags, and that WML tags are entered in lowercase.
Those new to ColdFusion should note that we are intermixing CF
tags and WML elements, but the CF tags are generating WML elements
that will be sent to the browser. The CF tags will be stripped out
after performing their assigned tasks on the server. All that the
user will receive is WML. (Which is also why you need not worry
about the CF tags being XML-compliant. You don't need to close the
<CFCONTENT> or <CFSET> tags, because they're never sent
to the browser.) Also, note that the page is a .cfm file
(hello.cfm), rather than a .wml file.
A more practical example would be a page that processes a WML
form, where the user is asked to enter data that we can process on
the ColdFusion page on the server. First, I'll present a fragment
of the form, which is just a pure WML file. The only hint that
something more is going on is the <go> element that passes
control to a ColdFusion page when the accept action is triggered
(by the user pressing their "OK" button [or equivalent] on the
phone):
<card>
<do type="accept">
<go href="wml_action.cfm"
method="post">
<postfield
name="symbol" value="$(symbol)"/>
</go>
</do>
<p>
Enter stock symbol:
<input name="symbol"
format="4A"/>
</p>
</card>
This is clearly quite different from an HTML form. For a start,
there is no <FORM> tag in WML, and the code used to display
the form and that for sending the form to the server are distinct.
You can also see the use of internal variables within WML
(something HTML can't do) when we use the form data gathered by the
<input> element and then passed by the <postfield>
element. Finally, notice that the <input> element offers an
opportunity to provide an expected format for the data entered,
which is another benefit over HTML.
The ColdFusion page for processing the form (referred to above
as wml_action.cfm) could be:
<CFCONTENT TYPE="text/vnd.wap.wml"><?xml
version=1.0"?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"
"http://www.phone.com/dtd/wml11.dtd">
<wml>
<card>
<p>
The stock
symbol selected was:
<CFOUTPUT>#form.symbol#</CFOUTPUT>
</p>
</card>
</wml>
This is still a simplistic example, since we're not really doing
much with the form data, but you can see how the form data (the
symbol variable in WML) is passed to and made available within the
ColdFusion program (as the CF variable form.symbol).
We could also have passed the form variable to a query, to find
all the records in some database table that match the stock
symbol.
Where to go From Here?
From here, it's a simple matter of placing valid CF tags in the
page to create proper WML, or to perform server processing
(updating databases, generating e‑mail, communicating with other
applications or web sites, etc.) on behalf of the user.
If you're new to WML, remember that you must put valid WML code
on the page. You must learn WML, and the differences between it and
HTML, before you can begin coding effectively. And if you're new to
CF, you should really learn as much as you can about the subject.
I've only brushed the surface in this paper, and there are dozens
more tags and hundreds more functions at your disposal. Some of the
more important CF components you should study for creating wireless
applications include:
Ø CFMAIL (for generating
e-mail)
Ø CFSCHEDULE (for creating
notifications and other scheduled content)
Ø CFSEARCH (for performing
text searches through document indexes)
Ø Session, client,
application, and server variables (to perform state management)
Ø Query caching (to improve
query processing for frequently referenced data)
Combining WML and CF expertise will open up worlds of
possibility for you. As well as the things I've mentioned so far,
interesting combinations of CF and WML include creating
table-formatted displays of data, dynamically generating pick
lists, and much more.