Mark Wutka shows you how to create a wireless Web application using Java Server Pages.
This article is excerpted from Special Edition Using Java Server Pages and Servlets (2000, Que).
Over the past few years, mobile phone networks have been switching from analog to digital transmission. In addition to getting better reception, digital PCS phone users are finding that their little phone is more than just a glorified walkie-talkie. Digital transmission makes it much easier for the phone network to pass data as well as voice traffic through the airwaves. Although this was possible with an analog system, it was much more complicated.
PCS phones now come with tiny Web browsers that work over a "wireless Web." This wireless Web is not quite the same as the Web you use from your home computer. Although there are gateways bridging the two Webs together, the data traffic on the wireless Web uses the Wireless Application Protocol (WAP) to transmit data. Without getting bogged down in the details, the wireless networks don't have the same data capacity as the rest of the Internet. WAP was designed to accommodate the low bandwidth and reduced connectivity restrictions of wireless networks.
Most wireless Web sites use the Wireless Markup Language (WML) which is a form of XML. That is, WML is defined using an XML Document Type Definition (DTD) and is specifically designed to accommodate the small browsers embedded inside PCS phones. Because many Web providers aren't yet willing to take the time to create a WML version of their site, it is possible to create an HTML-to-WML translator to make most of the Web available to PCS phones. Unfortunately, although this idea sounds good, very few Web sites translate well because of their layout. It is better to create a scaled-down version with WML.
One of the disadvantages of the gateway approach is that the WAP gateway can become a huge bottleneck. All the requests from the wireless network must funnel through the WAP gateway, which must keep track of connections on both networks.
Although there are no WAP servers yet that provide a servlet API, you may soon be able to write servlets and JSPs specifically for wireless networks. In the meantime, however, you can use JSP and servlets to create WML content that is accessed through a WAP gateway.
The following listing shows a simple "Hello World" application for a wireless device:
<%@ page language="java" contentType="text/vnd.wap.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="hello"> <p> Hello Wireless World! </p> </card> </wml>
The first thing you may notice in WirelessHello.jsp is that the content type is not text/html. Of course, it isn't text/wml either, which would probably make a lot more sense than text/vnd.wap.wml, but that's the way it is.
When you get past the <%@page line, the file looks like a typical XML file. You must always have the <?xml header. The <!DOCTYPE tag isn't strictly necessary for all XML pages, but if there is a DTD defined for the XML you are generating, you should include it.
Now you get into the actual WML code. The <wml> tag is the root tag of a WML document. Everything else must be enclosed within it. As you will see in a moment, a WML page consists of a number of cards, which are really like little pages. The WirelessHello page contains a single card named hello. The one tag that is familiar from the HTML world is the <p> tag defining a paragraph.
Remember that because WML is an XML-based markup language, a tag must always have a closing tag or must end with />. You must remember to use a closing </p> tag for paragraphs and to put a / in the <br/> tag to insert a line break.
Viewing the Wireless Hello Page
You don't need a wireless Web-enabled phone to test out your wireless Web pages. You can get a WAP phone simulator from some of the vendors that make phones and phone software. Phone.com provides the wireless Web browser software for a number of phone manufacturers and also makes a WAP gateway. You can download its UP.SDK development kit that includes a nice phone simulator. The Phone.com Web site is at http://www.phone.com/. Follow the link for developers, and you will see what Phone.com has to offer.
In case you're wondering why Phone.com's SDK is called UP.SDK, the company was once called Unwired Planet, thus the UP in UP.SDK.
Nokia, the well-known phone manufacturer, also has a phone simulator. Go to http://www.forum.nokia.com, and then follow the link for WAP developers. The Nokia phone simulator is in the Nokia WAP toolkit.