Another problem confronting developers considering dynamic WAP
applications is how to serve multiple clients. How do you know if
your visitor is an HTML or WML browser? How do you code your
program to serve appropriate content?
You have a few choices. Any will work, and the choice will
depend on your application, skill set, available resources, and
priorities.
The first thing to be aware of is that you can indeed detect the
type of browser that is visiting your site and running a given
program. Browser detection in ColdFusion is discussed in the next
section. Assuming you know the type of browser, then, what are your
choices?
One approach is to have a given server program tailor its output
to a range of different markup languages, depending on the type of
browser being used to access it. In other words, a single program
might be designed to provide a list of stocks. It could be
programmed to provide the list in HTML, to HTML browsers, and in
WML to WML browsers.
As elegant as it may seem, this approach may not always be
appropriate. Developers who have already written an application in
HTML may hope to save development time by creating multi-purpose
programs to serve both clients in a single program, but given that
the output requirements for WAP browsers are generally quite
different from those for HTML browsers, the effort to ensure
appropriate display for both types of browsers may negate the
potential time savings of having a single program.
A better approach is to have a front-door page that detects the
type of browser visiting your site, and then directs the user to a
set of pages that serve the appropriate result. If the results
being sent to the browser really are quite different depending on
the type of browser, this may be the more appropriate approach.
Another still more elegant result would be to split programs
into processing and presentation logic, and have the processing
component pass results to the presentation component for display to
the user as appropriate. As ColdFusion is more a procedural
language than an object-oriented one, this sort of segregation may,
again, be more trouble than it's worth. It's worth investigation,
though.
The bottom line is that there's nothing in ColdFusion to prevent
you choosing any of these three approaches. There are even specific
features that can be used to make each approach easier depending on
your preferences, some of which are discussed in this paper
(browser detection and redirection) while others are left to the
reader to investigate (custom tags, calling objects).
Browser Detection
Many people are interested in doing browser detection, to
recognize if the visitor is a WML or an HTML browser. While I
recommend that you think twice about trying to serve WML and HTML
from within the same template (for the reasons explained
above in Serving Multiple Clients), it's perfectly
reasonable to want to detect the browser type and send the user to
a page suitable for processing by that kind of browser.
One of the most reliable means of doing that is to detect what
MIME type the browser is expecting. HTML browsers will expect an
HTML MIME type, and WML browsers will expect a WML MIME type.
(There's a chance that someday, HTML browsers will permit and
emulate processing of WML decks, but until then this is a suitable
approach.)
Fortunately, the browser reports the type of pages it expects in
a CGI variable called http_accept, and CF can access that variable.
The CF code to detect and redirect a page based on the browser
would be just:
<CFIF cgi.http_accept contains "text/vnd.wap.wml">
<CFLOCATION url="/wml/index.cfm">
</CFIF>
Notice that the code is also using a CF tag called
<CFLOCATION>, which can be used to tell any CF program to
pass control to another page or program. Placing this code inside a
page at the front of your site, such as index.cfm, would allow that
page to detect and redirect WML browsers to a suitable WML-encoded
page.