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" 
"
<wml>  
  <card id="MyFirstCard" title="First Card">    
    <p align="center">
      My First WML Example
    
  </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

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" 
"
<wml>
  <card id="city" title="Ottawa Guide">
    <p align="center">
    

<a title="Business" href="#business">Business</a> <a title="Education" href="#education">Education</a> <a title="Entertainment" href="#entertainment">Entertainment</a> <a title="Restaurants" href="#food">Restaurants</a> <a title="Shopping" href="#shopping">Shopping</a> <a title="Travel" href="#travel">Travel</a> <a title="Weather" href="#weather">Weather</a>

</card> <card id="food" title="Restaurants">

<img src=" alt="logo"/>

<a title="Canadian" href="#canadian">Canadian</a> <a title="Chinese" href="#chinese">Chinese</a> <a title="Continental" href="#continental">Continental</a> <a title="French" href="#french">French</a> <a title="German" href="#german">German</a> <a title="Greek" href="#greek">Greek</a> <a title="Italian" href="#italian">Italian</a>

</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.

Listing 3 shows an example of a simple servlet that displays the current date and time on a wireless device when invoked.

Listing 3. MobileDate.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 * This is a simple servlet that will run on a cell-phone. It displays the   
 * current date and time. 
 *
 * @author <a href=" H. Mahmoud</a>
 */
public class MobileDate extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
      // set content type for wireless data
      response.setContentType("text/vnd.wap.wml");
// get the communication channel with the requesting client
      PrintWriter out = response.getWriter();
      // write the data
      out.println("<?xml version="1.0"?>");
      out.println("<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"");
      out.println(" "");
      out.println("<wml>");
      out.println("<card title="MobileDate">");
      out.println(" <p align="center">");
      out.println("Date and Time Service
");
      out.println("Date is: "+ new java.util.Date());
      out.println("");
      out.println("</card>");
      out.println("</wml>");
  }
}

Most of the preceding code uses pure WML tags, with the exception of the line:

Response.setContentType("text/vnd.wap.wml");

This line ensures that the correct MIME type is set for the WML document. Please refer to the Web Server Configuration section, which follows, for more information on how to add MIME types. The rest of Listing 1 basically outputs a WML document. When this servlet is invoked from a mobile phone, it displays the current date and time as shown in Figure 8. For this example, I have used Apache and JServ.

Figure 8. A servlet running on the Ericsson’s simulator

By the way, cookies are useful for maintaining state and keeping track of users’ sessions. Cookies are part of the WAP specification, but unfortunately, are not yet implemented by all WAP browsers. The Nokia 7110 doe s not yet support cookies. However, Phone.com’s UP.Simulator does support them.

WAP and JSP

Java Server Pages lets you embed Java statements within HTML documents. When JSP is invoked, it is compiled into a Java servlet and executed by the server to create a dynamic HTML document. In the case of WAP, however, you want to create dynamic WML documents. Therefore, developing WAP applications using JSP can be done easily once you know the syntax of WML.

The following example is similar to the preceding servlet example; it displays the current date and time on a wireless device.

Listing 4. mobiledate.jsp

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"
<%
response.setContentType("text/vnd.wap.wml");
out.println("<wml>");
out.println("<card title="MobileDate">");
out.println(" <p align="center">");
out.println("Date and Time Service
");
out.println("Date is: "+ new java.util.Date());
out.println("");
out.println("</card>");
out.println("</wml>");
%>

Again, you need to set the MIME type correctly to make sure that the mobile phone browser is able to parse the contents. The following line sets the MIME type to a WAP/WML document:

<% response.setContentType("text/vnd.wap.wml") %>

Once this JSP document is invoked, it displays the current date and time as shown in Figure 9.

Figure 9. JSP page running on Ericsson’s simulator

JAWAP

JAWAP (which was initially known as JAFFA) is Ericsson’s Java Framework for WAP. It is based on RMI and servlets. I personally found JAWAP to be a bit complex to use; for example, you have to develop several classes to get a simple application up and running. Further, it doesn’t seem to be stable. Using servlets or JSP to develop WAP applications is much easier than using JAWAP. If you are curious about JAWAP, however, you can download it from Ericsson’s Website in Resources.

Web server configuration

When a regular Web browser receives a page, it has to distinguish between HTML, image data, audio, and video. To enable that, with every response from the Web server, a piece of header information with every file comes down to the browser. This piece of information is known as MIME, which stands for Multipurpose Internet Mail Extension. Some of the common MIME types include text/html for HTML files and image/gif for gif files.

To enable the Web server to serve WAP documents, it needs some new MIME types. The types that WAP requires are:

  • text/vnd.wap.wml for .wml files (WML source files)
  • application/vnd.wap.wmlc for .wmlc files (WML compiled files)
  • text/vnd.wap.wmlscript for .wmls files (WMLScript source files)
  • application/vnd.wap.wmlscriptc for .wmlsc files (WMLScript compiled files)
  • image/vnd.wap.wbmp for .wbmp files (wireless bitmaps)

Adding these MIME types varies from server to server. Check the documentation for the Web server you are running, and you’d be surprised how easy it is to add these new MIME types. In Apache, the preceding MIME types should be added to the httpd.conf file.

WAP emulators and development tools

In order to access WAP services, you need a WAP-enabled phone like the Nokia 7110, Siemens S25, Ericsson R380, or Alcatel OneTouch. However, as far as I know, there are no WAP-enabled phones in the North American market yet. The Nokia 7110, the first WAP-enabled phone, has been available in Europe, Asia, and Africa for several months now. The joke in the American wireless market is that WAP stands for Where Are the Phones. Still, I think you will start seeing WAP-enabled phones in the American market sometime this summer.

If you are in North America and you’d like to start with WAP, you can use an emulator for now. There are several emulators available. In this article, I have used the ones from Ericsson and Nokia. But there are others available from Motorola and Phone.com. See Resources.

As for development tools, I have used Nokia’s WAP ToolKit and Ericsson’s WapIDE, and I liked them both. They are basically used to assist in developing WAP applications. But the major advantage of them is that they come with WAP simulators with which you can test your WAP application. Again, other development tools are available in Resources.

Conclusion

WAP is an open specification for delivering Internet content to wireless devices. In this article, you have learned the basics of WAP, WML, and WMLScript. I have provided examples of sample WAP applications to give you a flavor of WAP, and I have demonstrated how you can develop WAP applications in Java using servlets and JSP. WAP is a new technology with a promising future (or so I hope). If you would like to discuss your experiences or ask questions about using WAP and Java, please join my short-term community discussion on this topic. Hope to hear from you there!

Qusay H. Mahmoud lives in Ottawa, Canada. He
provides Java and WAP
consulting and training services. Qusay has published dozens of
articles on Java, and is the author of Distributed Programming
with Java (Manning Publications, 1999).

Source: www.infoworld.com