We have all written applications for conventional office computers, be they mainframes or laptops. But how about wireless applications? How about running your applications on something like a cell phone? That is what Wireless Application Protocol (WAP) is all about. We will discuss WAP and take you through a simple example of programming for the wireless world.

WAP basics
Let’s start with a little theoretical background. WAP is used to connect WAP-enabled wireless devices (such as WAP phones) to a WAP gateway and allow them to use WAP applications. A collection of WAP applications is usually available in what I call a WAP portal.

In 1997, Nokia, Motorola, Ericsson, and Phone.com joined forces to found the WAP Forum. As with many standards, WAP is ever evolving and fighting for mass-market acceptance.

This article won’t focus on WAP itself but instead on the tools and languages you can use to build WAP applications. In particular, we’ll look at the Wireless Markup Language (WML), and I’ll show you how to use Delphi 6 to create dynamic WML for WAP applications.

For the latest news on WAP and relating tools and toolkits, you should go to Nokia’s site where you can also download the Nokia Mobile Internet Toolkit version 3.0 or the older Nokia WAP Toolkit version 2.1, which I used to simulate a WAP phone on my laptop.

We can use Delphi 6 to create WAP applications, which are just like regular Web server applications. The only difference is that a regular Web server application typically returns HTML, while a WAP Web server application returns WML. Although WML is based on XML, and it sometimes looks like HTML, it’s quite different in some respects and is much stricter when it comes to small syntax errors.

Delphi 6
Delphi 6 has built-in support for XML and HTML, but there are no special WML components. Fortunately, we can still use the regular Web server application wizard. However, we have to generate the WML content ourselves, as I’ll show in the steps that will follow. For a more detailed overview of the WebBroker architecture I’m using here (available in Delphi, C++Builder, and Kylix), you may want to check out some of the papers on my own Web site.

The visual content that can be displayed on a WAP device is called a card. A WML document is called a deck and consists of one or more cards. The WML card starts with a two-line XML definition, followed by the wml-tag itself (an opening and closing tag, as you’ll see in Listing A). Each individual card is placed in a card-tag and consists of a visual (display) part and a nonvisual (action) part. The display part is placed between <p>…</p> tags, and the action part usually consists of a <do>…</do> pair.

As an example, let’s consider a WML card that asks the user to input an amount of money in either (Dutch) Guilders or in Euros, to be used in a little Guilder-Euro (or Euro-Guilder) conversion demo application. This card appears in Listing A.

Using the Nokia WAP Toolkit, we can display this WML card on the WAP simulator, as shown in Figure A.

Figure A
The WML in Listing A will look like this on the simulator.

The display
The display part of the code results in a line that says Please enter:, followed by two more lines that can be used to input Guilders or Euros. Both input fields have a format *N (meaning an unlimited amount of numerical characters).

The action
The action part is associated with the “prev” button and has a label called “Convert.” Inside the do tag, the go tag will execute an external URL (or WAP application) called wap.exe (with the /convert PathInfo), which is located in the cgi-bin directory. We’ll use Delphi 6 to build this WAP Web server application in a moment.

Using the POST protocol, two fields and values will be passed to this WAP application: a field named Guilder, which has a value that belongs to the field named Guilders in the display part, and a field named Euro, which has a value connected to the Euros field in the display part.

Configuring the Web server
To configure your Web server so that it can understand WML output (and WBMP bitmaps), you need to add two special MIME types. To do this, go to the configuration dialog box for your Web server (like IIS5 on Win2K), select the HTTP Headers tab, and click on the File Types button to register text/vnd.wap.wml for .wml pages (decks) and image/vnd.wap.wbmp for .wbmp images (Figure B).

Figure B
Registering needed file types

Optionally, you may want to register text/vnd.wap.wmlscript (for WMLScript) as well, but I’m not using WMLScript this time.

Producing WML
Now that we’ve set the stage, it’s time to start Delphi 6 (or Kylix) and build a Web server application that can produce WML. The type of Web server application doesn’t really matter, but since Delphi and Kylix have only one in common, I’ve created a CGI executable in my example that I named wap42. Remember that any Web server application can produce WML code, so feel free to experiment with an ISAPI/NSAPI or Apache DSO for a real-world situation. Right-click inside the Web module and create two Web action items: one with the /convert PathInfo and one without a PathInfo, which will be the default Web action item. The Web action item editor should look like that in Figure C.

Figure C
Web action items

We’ll start with the default Web action, so select that particular item, move to the OnAction event handler of the Object Inspector, and write the code in Listing B.

The first statement concerns the ContentType, which by default is text/html, so we must explicitly assign it to text/vnd.wap.wml. After we’ve set the ContentType, we must assign the Response.Content itself. This starts with the two-line XML header and then follows with the WML card we saw earlier for the Guilder-Euro conversion. The only difference is that this time, we’ll dynamically generate the WML (instead of using a static WML card).

WAP input
Using the Nokia WAP Toolkit, we can now load http://localhost/cgi-bin/wap.exe and see the output of the first figure again. If we then click on the Edit button, we can enter a value for Guilders (or Euros, depending on the field that was selected when we clicked on Edit). You can see this in Figure D.

Figure D
Entering a value to convert

If you click OK and then click Convert, the conversion starts, which means that the /convert PathInfo item of the wap.exe WAP server application is called. Of course, we have to implement it first, which is done with the code shown in Listing C.

The only thing that’s new is the part where we collect the values of Guilder and Euro using the ContentFields property of the Request object. Since ContentFields.Values returns a string, we must use StrToInt to convert it to an integer (or StrToIntDef to pass a default value in case the conversion fails due to an empty string).

If the value of Guilder is 0, we probably entered a Euro value and should derive Guilder from Euro. Otherwise, we should derive the value for Euro from the value of Guilder.

The result is displayed by producing dynamic WML as we’ve done before. Note that we’re using an action part again (or this would be the end of the WAP session), restarting the wap.exe application if the user clicks on the Home button. The final output is shown in Figure E.

Figure E
The result

Going mobile
I’ve shown you how to set up your Web server to register WML and WBMP file formats, how to use the Nokia WAP Toolkit, how to use Delphi 6 and the WebBroker Technology to produce dynamic WML, and how to obtain and process WML input from WAP devices. Now it’s up to you to play some more.

Additional resources
You can visit the WAP Forum Web site for more information about the latest version of WAP 2.0. I also recommend visiting the Forum Nokia for the Nokia Mobile Internet (WAP) Toolkit, as well as reading Essential WAP for Web Professionals, published by Prentice Hall (ISBN: 0-13-092568-3). Finally, you might be interested in a recent article about WAP bashing that was published on the IBM developerWorks Web site.