WAP for Java developers

Develop WAP applications with Java servlets and JSP

Most Internet technologies have been designed for desktop and large computers running on reliable networks (with relatively high bandwidth). Handheld wireless devices, however, have a more constrained computing environment compared to desktop computers. Handheld devices tend to have less memory, less powerful CPUs, different input devices, and smaller displays. Further, wireless networks have less bandwidth and more latency compared to wired computer networks.

WAP, the Wireless Application Protocol, was designed to take advantage of the several data-handling approaches already in use. WAP integrates the Handheld Device Markup Language (HDML) and the Handheld Device Transport Protocol (HDTP) developed by Unwired Planet (now known as Phone.com), as well as Nokia's Smart Messaging Protocol (SMP), and Ericsson's Intelligent Terminal Transfer Protocol (ITTP). WAP services can be hosted on Web servers using technologies such as Java servlets and JavaServer Pages (JSP). In this article, I show you that WAP and Java are complementary, not competing, technologies. WAP is meant for cellular phones, and Java aims at more sophisticated network terminals.

The WAP platform is an open specification that addresses wireless network characteristics by adapting existing network technologies (and introducing new ones where appropriate) to the special requirements of handheld wireless devices. Therefore, WAP intends to standardize the way wireless devices (mobile phones, PDAs, and so forth) access Internet data and services. WAP's reuse of existing Internet protocols will ease the development of WAP services for Java and other Web developers.

To facilitate the delivery of Internet data to wireless devices will certainly lead to the introduction of new technology. For example, wireless devices have small screens compared to desktop computers, and therefore HTML (which is visually rich) is not appropriate for small screens. The small screen and the computing power, which is increasing, represent a limiting factor in running complicated Java applications with intensive graphics. However, efforts are being made for having a compact subset of the Java language suitable for mobile applications. In this article, you learn how to develop WAP applications using Java servlets and Java Server Pages.

But first, I describe the WAP platform and how it incorporates several technologies that are similar to existing ones but that have been designed specifically for wireless devices.

The WAP architecture

The WAP standard defines two essential elements: an end-to-end application protocol and an application environment based on a browser. The application protocol is a communication protocol stack that is embedded in each WAP-enabled wireless device (also known as the user agent). The server side implements the other end of the protocol, which is capable of communicating with any WAP client. The server side is known as a WAP gateway and routes requests from the client to an HTTP (or Web) server. The WAP gateway can be located either in a telecom network or in a computer network (an ISP). Figure 1 illustrates an example structure of a WAP network.

Figure 1. The WAP network structure

In Figure 1, the client communicates with the WAP gateway in the wireless network. The WAP gateway translates WAP requests to WWW requests, so the WAP client is able to submit requests to the Web server. Also, the WAP gateway translates Web responses into WAP responses or a format understood by the WAP client.

The WAP programming model

The WAP programming model is similar to the Web programming model with matching extensions, but it accommodates the characteristics of the wireless environment. Figure 2 illustrates this model.

Figure 2. The WAP programming model

As you can see, the WAP programming model is based heavily on the Web programming model. But how does the WAP gateway work with HTML? In some cases, the data services or content located on the Web server is HTML-based. Some WAP gateways could be made to convert HTML pages into a format that can be displayed on wireless devices. But because HTML wasn't really designed for small screens, the WAP protocol defines its own markup language, the Wireless Markup Language (WML), which adheres to the XML standard and is designed to enable powerful applications within the constraints of handheld devices. In most cases, the actual application or other content located on the Web server will be native WAP created with WML or generated dynamically using Java servlets or JSP.

In HTML, there are no functions to check the validity of user input or to generate messages and dialog boxes locally. To overcome this limitation, JavaScript was developed. Similarly, to overcome the same restrictions in WML, a new scripting language known as WMLScript has been developed. I'll cover more on WML and WMLScript in later sections.

The WAP protocol stack

To minimize bandwidth requirements, and guarantee that a variety of wireless networks can run WAP applications, a new lightweight protocol stack called the WAP protocol stack was developed. Figure 3 illustrates this stack protocol and also shows a comparison to a Web-based protocol stack. (The layer acronyms can be translated to Wireless Application Environment, Wireless Session Protocol, Wireless Transaction Protocol, Wireless Transport Layer Security, and Wireless Datagram Protocol, respectively).

Figure 3. The WAP protocol stack

The WAP protocol stack has four layers: session layer, transaction layer, security layer, and datagram layer. Note that the WAP protocol is designed to operate over a variety of bearer services, including Code Division Multiple Access (CDMA), Cellular Digital Packet Data (CDPD), and so forth.

WML

The Wireless Markup Language (WML) is an XML-based markup language that was designed to describe how WAP content is presented on a wireless terminal. WML differs from HTML in the following:

  • WML was specifically designed for wireless terminals with a target screen that is only a few lines long and about an inch wide.
  • WML is case sensitive, and all tags and attributes should be in lowercase.
  • Unlike HTML, WML is unforgiving of incorrectly nested tags.
  • WML doesn't assume that a "QWERTY" keyboard or a mouse is available for user input.

Based on these differences, WML provides a smaller, telephony-aware set of tags that make it more appropriate than HTML for handheld wireless terminals. Similar to HTML, though, with WML you can give the user input options and specify how the user agent should respond when, for example, the user presses a key.

The genesis of WML

The basic unit of WML is the card that specifies a single interaction between the user and the user agent. Multiple cards are grouped together in decks. A deck is the topmost element of a WML document. When the user agent receives a deck (by downloading the complete deck), it activates only the first card in the deck. Listing 1 shows a sample WML document with a single card:

Listing 1. Example1.wml

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>  
  <card id="MyFirstCard" title="First Card">    
    <p align="center">
      My First WML Example
    </p>
  </card>
</wml>

When viewed on a WAP-enabled phone, Listing 1 is rendered and displayed as shown in Figure 4.

Figure 4. The result of Example1.wml

As I mentioned before, WML is based on XML. Therefore, a deck has to be a valid XML document, which implies that a WML document (as the one shown in Listing 1) should start with the standard XML header and the reference to the WML DTD. The <p> tag delimits a paragraph and must be used to wrap any text you want to display.

Now, take a look at an example that has two cards and uses other WML features. Listing 2 shows the Ottawa City Guide example.

Listing 2. Example2.wml

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="city" title="Ottawa Guide">
    <p align="center"></p>
    <p>
      <a title="Business" href="#business">Business</a><br/>
      <a title="Education" href="#education">Education</a><br/>
      <a title="Entertainment" href="#entertainment">Entertainment</a><br/>
      <a title="Restaurants" href="#food">Restaurants</a><br/>
      <a title="Shopping" href="#shopping">Shopping</a><br/>
      <a title="Travel" href="#travel">Travel</a><br/>
      <a title="Weather" href="#weather">Weather</a><br/>
    </p>
  </card>
<card id="food" title="Restaurants">
    <p><img src="food.bwmp" alt="logo"/></p>
<p>
<a title="Canadian" href="#canadian">Canadian</a><br/>
      <a title="Chinese" href="#chinese">Chinese</a><br/>
      <a title="Continental" href="#continental">Continental</a><br/>
      <a title="French" href="#french">French</a><br/>
      <a title="German" href="#german">German</a><br/>
      <a title="Greek" href="#greek">Greek</a><br/>
      <a title="Italian" href="#italian">Italian</a><br/>
    </p>
</card>
</wml>

This example was developed using the Ericsson WapIDE (WAP integrated development environment). WapIDE is shown in Figure 5 along with the output of Listing 2.

Figure 5. Ericsson's WapIDE. Click on thumbnail to view full image (53k)

The example in Listing 2 has two cards: city and food. When a device receives this deck, it loads the first card automatically as shown in Figure 6.

Figure 6. The first card

Once the city card is loaded, you can navigate through it using the soft key on your device. Figure 7 shows what happens when the Restaurants option is selected.

Figure 7. The Restaurants option

Navigation in this example is implemented using anchors (<anchor>), which are the WML counterpart of <A> tags in HTML. An anchor can be defined as:

<anchor>
   Go to food
   <go href="#food"/>
</anchor>

This is identical to:

<a href="#food">Go to food</a>

In the preceding example, I have used <a> instead of <anchor> because it allows more efficient tokenization. The same thing can be accomplished using the <do> element. For example, the preceding anchor is identical to:

<do type="accept" label="Go to food">
  <go href="#food"/>
</do>

Therefore, <anchor>, <a>, and <do> can all contain a task. The only difference is that anchors do not appear as an option in the soft-key menu, but are rendered as links (similar to HTML links) in WML.

Graphics in WML

Like HTML, WML has an <img> tag to support images. However, I should note a couple things about the <img> tag in WML. First, the alt attribute of the <img> tag is mandatory. Second, it is one of the few tags in WML that doesn't have a closing tag. Therefore, it needs to have its own closing mark (/) in the tag, as shown in Listing 2.

In the preceding food card, I am using the image food.wbmp. The new .wbmp format stands for wireless bitmaps, which is specifically designed for wireless devices with a small monochrome screen. You can use a package like Paint Shop Pro to create your graphics, but to be able to save or edit the .wbmp format, you need a converter or a plugin. See Resources for URLs.

There are other tags defined in WML, such as <template>, and tags for user input. More important, WML integrates variables in a different way from that used in HTML and JavaScript.

WMLScript

WMLScript, which is based on ECMAScript (the standard for JavaScript), is a language that you can use to provide programmed functionality to WAP applications. It is part of the WAP specification, and it can be used to add script support to the client. The main difference between WMLScript and ECMAScript is that WMLScript is compiled into byte code before it is sent to the client. The main reason for this is of course to cope with the narrowband communication channels and to keep client memory requirements to a minimum.

You can use WMLScript to check the validity of user input, but most important, you can use it to generate messages and dialogs locally, so error messages and confirmations can be viewed faster, and to access facilities of the user agent -- for example, it allows the programmer to make phone calls, access the SIM card, or configure the user agent after it has been deployed.

WAP and Java

WAP applications, which will be hosted on normal Web servers, can be written in WML and WMLScript. But you can also write them using existing Web technologies. Dynamic WML documents can be generated by CGI scripts, servlets, JavaServer Pages, Active Server Pages, Perl, Tcl, and so forth. In other words, you can implement your own first WAP service in Java, with the help of the WAP technologies described earlier. In this section, I am going to show you how WAP applications can be easily developed in Java using servlets and JSP.

WAP and servlets

Dynamic WML documents for wireless devices can be easily developed using Java servlets. Once you know the WML syntax, building WAP applications using Java servlets can be an easy task.

Related:
1 2 Page 1
Page 1 of 2