J2ME MIDP and WAP Complementary Technologies

Print-friendly Version

by Qusay H. Mahmoud February 2002 The Wireless Application Protocol (WAP) is an open specification that addresses wireless network characteristics by adapting several data-handling approaches already in use by Web protocols, and introducing new ones, where appropriate, to the special requirements of handheld wireless devices. The reuse of existing Web technologies eases the development of WAP applications, and make it similar to developing HTML-based Web applications since it is browser-based. Another approach to developing wireless applications is to use the Java 2 Micro Edition (J2ME) Mobile Information Device Profile (MIDP). MIDP is also an open specification that adapts existing technologies such as Java and the Web. Developing MIDP-based applications (also known as MIDlets) is similar, but not identical, to developing Java Applets in the sense they share a similar programming model. In either approach, Java plays an important role. In WAP, Java servlets and JavaServer Pages (JSPs) can be used to generate dynamic WAP content on the fly (not to mention that most WAP development tools are actually written in Java!), and in MIDlets, Java is the programming language for developing them. WAP and MIDP are not competing technologies, however. They are in fact complementary technologies as you will see here. If you are new to WAP, please see WAP for Java Developers, and if you are new to MIDP please see Getting Started with MIDP. This article presents a brief overview of the WAP and MIDP programming models, then it:
y y y

Gives a flavour of the effort involved in developing WAP applications and MIDlets Shows how to rewrite existing WAP applications as MIDlets Provides a look into how WAP and MIDP will be integrated together

The examples throughout this article were developed using Ericsson's WapIDE and Sun's J2ME Wireless Toolkit.

which is a reformulation of HTML 4. to overcome the same restrictions in WML.Programming Models The programming model of both. Similarly. WAP 2. JavaScript was developed. there are no functions to check the validity of user input or to generate messages and dialog boxes locally. which adheres to the XML standard and is designed to enable powerful applications within the constraints of handheld devices. the WAP protocol defines its own markup language. But because HTML wasn't really designed for small screens.0 as an XML application. To overcome this limitation. 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 JSPs. In most cases. The WAP Programming Model The WAP programming model is similar to the Web programming model with matching extensions for accommodating the characteristics of the wireless environment. Sample 1 shows the simplest WML document with a single card. a new scripting language known as WMLScript has been developed. in some cases the data services or content located on the Web server is HTML-based. In HTML.0 is moving towards XHTML. the WAP programming model is based heavily on the Web programming model. Figure 1 illustrates the WAP programming model. The basic unit of a WAP application in WML is the card that specifies a single interaction between the user and the user agent. WAP and MIDP are in many ways similar to existing programming models such as the Web and Java. . While WAP was not designed to use HTML. the Wireless Markup Language (WML). Some WAP gateways are capable of converting HTML pages into a format that can be displayed on wireless devices. Figure 1: The WAP Programming Model As you can see.

Sample 1: Hello. Note: It is natural for MIDlets to remain on the device till they are explicitly removed.wapforum.wml The MIDP Programming Model The MIDP programming model is a mix of the Java programming model and the Web programming model. MIDlets remain available for offline usage and in that sense MIDP supports disconnected operations. however. Sample 1 is rendered and displayed as shown in Figure 2. MIDlets run in a MIDlet management software (which is preinstalled on MIDP devices) that provides an operating environment for KVM and MIDlets where they run. Therefore. This is a big plus for entertainment applications such as games! . MIDlets do not get destroyed when they finish running.2//EN" "http://www. where an applet is described in an HTML file.dtd"> <wml> <card id="FirstCard" title="First Example"> <p> Hello World </p> </card> </wml> When viewed on a WAP-enabled phone. Figure 2: The output of Example1. Unlike applets.wml <?xml version="1. while applets run in a Web browser. MIDlets are developed using Java and compiled the same way you compile any Java application. Similar to applets. however. a MIDlet or a group of MIDlets (known as a MIDlet Suite) is described in a Java Descriptor (JAD) file.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.org/DTD/wml12. They remain installed on the device until they are explicitly removed.

It is the same idea with HTML-based applications and applets. . Applets however. public HelloMIDlet() { } public void startApp() { display = Display. 20.java importjavax. */ public void destroyApp(boolean unconditional) { } } Even to the untrained eye.lcdui. box = new TextBox("First Example". the basic unit of interaction is the screen. "Hello World".*. public class HelloMIDlet extends MIDlet { // The display for this MIDlet private Display display.setCurrent(box).microedition. The WML example shown earlier in Sample 1 can be rewritten as a MIDlet as shown in Sample 2. bring dynamic applications to the Web and it is the same thing with MIDlets. * In this case there is nothing to cleanup. as we mentioned earlier these are not competing technologies but complementing ones. // TextBox to display text TextBox box. importjavax. which encapsulates and organizes graphics objects and coordinates user input through the device. Sample 1 looks much simpler than Sample 2.getDisplay(this). 0). such as the Motorola/Nextel i85s.midlet.*. we are not trying to compare WAP applications with MIDlets here.In MIDlets. But remember. Sample 2 is displayed as shown in Figure 3. Sample 2: HelloMIDlet. } /** * Pause is a no-op since there are no background activities or * record stores that need to be closed. When running on a Java-enabled phone.microedition. */ public void pauseApp() { } /** * Destroy must cleanup everything not handled by the garbage * collector. display.

lists. A deck is the topmost element of a WML document.wapforum. The idea however. is not to compare which one is easier but rather to show you the effort involved. When the user agent receives a deck.2//EN" "http://www. it activates only the first card in the deck. Sample 3 shows a hypothetical guide for the city of Vancouver that consists for a deck composed of three cards.Figure 3: Output of HelloMIDlet Rewriting WAP Applications as MIDlets We now look at rewriting WAP applications as MIDlets.dtd"> <wml> <card id="city" title="Vancouver Guide"> <p> <a title="Food" href="#food">Food</a><br/> <a title="Education" href="#education">Education</a><br/> </p> </card> <card id="food" title="Restaurants"> <p> <a title="Canadian" href="#canadian">Canadian</a><br/> <a title="Chinese" href="#chinese">Chinese</a><br/> <a title="Greek" href="#greek">Greek</a><b/> </p> </card> . input fields. We discuss how to rewrite decks.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1. Sample 3: CityGuide.wml <?xml version="1.org/DTD/wml12. buttons. Decks Multiple cards are grouped together in decks. and WML with WMLScript.

*. List menu. List edu. Sample 4: CityMIDlet. a MIDlet usually consists of several screens. Once the city card is loaded. and Figure 4c shows what happens when the Education option is selected. String currentMenu.java importjavax. public class CityMIDlet extends MIDlet implements CommandListener { Display display. public CityMIDlet() { } . Sample 3 is rendered as shown in Figure 4a.*.<card id="education" title="Education"> <p> <a title="Schools" href="#schools">Schools</a><br/> <a title="Univerities" href="#universities">Universities</a><br/> </p> </card> </wml> When viewed on a WAP-enabled device.midlet. Figure 4a:City Figure 4b:Food Figure4c:Education Just as a WAP application may consist of multiple cards in a deck. static final Command nextCommand = new Command("Next".OK. Command. but only one screen at a time can be visible. Command. you can navigate through it using the soft key on your device.microedition. Figure 4b shows what happens when the Food option is selected. 2).BACK. static final Command backCommand = new Command("Back". importjavax. 2).microedition.lcdui. List food. The WAP application in Sample 3 can be written as a MIDlet as shown in Sample 4.

setCommandListener(this).getCurrent().addCommand(nextCommand).setCurrent(menu). Choice.IMPLICIT). . null).equals("food")) { mainMenu(). } public void eduMenu() { edu = new List("Education".append("Food". food.addCommand(backCommand).append("Greek".addCommand(backCommand). edu.getLabel(). Choice. } else if (currentMenu. food. } public void commandAction(Command c. edu. menu = new List("Vancouver Guide".append("Canadian". } public void foodMenu() { food = new List("Restaurants". switch(down. currentMenu = "main". menu.equals("Next")) { if(currentMenu. if(label.setCommandListener(this).append("Universities". edu. menu = null. currentMenu = "food". null).append("Education". null). case 1: eduMenu().equals("main")) { List down = (List) display.IMPLICIT).addCommand(nextCommand). currentMenu = "education". break. Displayable d) { String label = c.getDisplay(this). display. null).public void startApp() { display = Display.setCurrent(food). food. mainMenu(). break. food.setCommandListener(this).addCommand(nextCommand). edu. menu.setCurrent(edu). null). Choice.append("Schools". null). display. menu. } public void destroyApp(boolean unconditional) { notifyDestroyed(). food.equals("education")) { mainMenu().IMPLICIT).append("Chinese". null). } } else if (currentMenu. edu. } public void pauseApp() { display = null. menu. } public void mainMenu() { display.getSelectedIndex()) { case 0: foodMenu(). food.

equals("food")) { mainMenu(). navigation in the Vancouver city example is done using the <A> tag. On the other hand. Lists A list in a WAP application can either be of a single choice or multiple choice type. Figure 5: Output of CityMIDlet Navigation in WAP and MIDP As you can see from Sample 3. navigating between screens in MIDlets is done using commands. Sometimes I get lost between cards and would not know which key do I need to press to get a list of options.equals("Back")) { if(currentMenu. Single Choice Lists In a single choice list. In a sense. Sample 4 is displayed as shown in Figure 5. Unlike MIDP. } else if(currentMenu. Sample 5 shows the WML code for a single choice list. } } } } When running on a MIDP-enabled phone. commands in MIDP are similar to <A> since they are mapped to softkeys as well but personally I prefer the navigational mechanisms in MIDP much better than WAP.equals("education")) { mainMenu().} } else if(label. only one option can be selected. which will appear as an option in the soft-key menu and can be selected to move to the next card. . for example. WAP provides an ill-defined navigation and interaction model with some of its hidden navigational paths.

Form form. ChoiceGroup size.org/DTD/wml12.java importjavax. Figure 6: Single Choice List MIDP supports single choice lists as well. Sample 6: PizzaMIDlet. The Pizza example shown in Sample 5 can be written as a MIDlet as shown in Sample 6.midlet.microedition. .lcdui. importjavax.2//EN" "http://www.wapforum.Sample 5: Pizza. it looks as shown in Figure 6.microedition.dtd"> <wml> <card id="Pizza" title="Pizza Mobile"> <p> <b>Pizza Size</b> <select title="Size"> <option>Small</option> <option>Medium</option> <option>Large</option> </select> </p> </card> </wml> When rendered on a WAP-enabled phone.*. public class PizzaMIDlet extends MIDlet { Display display.*.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.wml <?xml version="1.

this example is displayed as shown in Figure 7.append("Medium". multiple items can be selected all at once. size.wml <?xml version="1.append("Large". null). null). size. Sample 7: Toppings. Choice. form.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1. size = new ChoiceGroup("Pizza Size".getDisplay(this). Sample 7 shows a WML example of a multiple list for selection toppings for a Pizza.dtd"> <wml> <card id="top" title="Pizza Toppings"> . Figure 7: Single Choice List in MIDP Multiple Choice Lists In a multiple choice list. } } When it runs on a MIDP-enabled device.2//EN" "http://www. size. For example. display.setCurrent(form).append("Small". form = new Form("Pizza Mobile"). } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(). null).org/DTD/wml12.EXCLUSIVE).wapforum.append(size).public PizzaMIDlet() { } public void startApp() { display = Display.

java importjavax. As you can see.microedition. . In this example. Sample 8: ToppingsMIDlet.midlet. importjavax.*. however.*. it will be rendered and displayed as shown in Figure 8.<p> <b>Select Toppings</b> <select title="Toppings" multiple="true"> <option>Mushrooms</option> <option>Black Olives</option> <option>Extra Cheese</option> <option>Pineapple</option> </select> </p> </card> </wml> When it runs on a WAP-enabled device. public class ToppingsMIDlet extends MIDlet { Display display. all that needs to be done to change from a single choice list to a multiple choice list is to use MULTIPLE instead of EXCLUSIVE.microedition.getDisplay(this). Form form. Figure 8: Multiple Choice List in WML The WML Toppings example can be written as a MIDlet.lcdui. we have used a ChoiceGroup instead of a List just for illustrating the rich family of GUI components available in MIDP. public ToppingsMIDlet() { } public void startApp() { display = Display. ChoiceGroup size.

null).2//EN" "http://www. This is done in Sample 9.MULTIPLE). Choice. form. display. Now we will see an example of a WAP application that requires the user to enter their name and phone number. } } When the ToppingsMIDlet runs on a MIDP-enabled device.append("Pineapple".dtd"> <wml> <card id="form" title="Pizza Delivery"> . Figure 9: Multiple Choice List in MIDP Input Fields Applications may need to input from users. null).org/DTD/wml12.append("Black Olives". Sample 9: Delivery.wapforum.setCurrent(form). size. it is displayed as shown in Figure 9. } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(). form = new Form("Pizza Toppings").0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1. size.wml <?xml version="1.size = new ChoiceGroup("Select Toppings".append(size). null). size.append("Mushrooms".append("Extra Cheese". null). size.

When the user clicks on the menu.microedition. the Submit button shows up and the user can select it as shown in Figure 10b.microedition.buttons.MIDlet. When the Submit button is selected. Figure 10a: input Figure 10b: Submit button This example can be written as a MIDlet as shown in Sample 10. private TextField name. .*.<p> <b>Enter your name and address</b><br/> <input type="text" title="Name: " name="name"/><br/> <input type="text" title="Phone: " name="phone"/><br/> </p> <p> <do type="accept" label="Submit"> <go href="#db"/> </do><br/> </p> </card> </wml> This example uses another form of navigation -. private TextField phone. When this applications runs on a WAP-enabled browser. importjavax. the #db card (which is not shown in this example) will be loaded. private Form form. Sample 10: DeliveryMIDlet.java import javax. it is displayed as shown in Figure 10a.midlet. private Command submit.lcdui. public class DeliveryMIDlet extends MIDlet { private Display display.

you do not need to press a key multiple times to get a specific digit.ANY). "". form.getDisplay(this).append(name). 10.PHONENUMBER).ANY signals that any character can be entered. } public void startApp() { display = Display. display. form = new Form("Pizza Delivery"). This makes MIDlets visible and more user friendly. If you run this MIDlet on a MIDP-enabled device. form.setCurrent(form). In comparison with the WML example. This is useful as on some phones this can be interpreted as when a text field requires a phone number then the phone keys can only be used to enter digits and therefore input can be entered faster.public DeliveryMIDlet() { name = new TextField("Name:". 10. it will be displayed as shown in Figure 11. } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(). "". TextField. Command. MIDP is more explicit in what kind of input is required. } } As you can see.addCommand(submit).PHONENUMBER signals that only digits can be entered. note that in the case of MIDlets the commands are shown on the same screen and therefore there are no hidden paths. form. Figure 11: Input fields in MIDP Validating Input with WMLScript . and TextField. phone = new TextField("Phone:". 2). TextField. In other words.append(phone). For example TextField.OK. submit = new Command("Submit".

login incorrect. can this be rewritten as a MIDlet.xml">> <wml> <card id="passwd" title="password:"> <p> Enter Password: <input type="text" name="passwd"/> </p> <do type="accept" label="GetIn"> <go href="login. The question that may come to mind is what about WMLScript? If a WML page uses a WMLScript page.toString(passwd) == "99") { WMLBrowser. and the WMLScript code (which is responsible for checking and verifying the input) is shown in Sample 12. } }.wmls extern function validatePASSWD(passwd) { if (String.alert("Error. .So far we have seen how to rewrite WML applications as MIDlets. The answer is yes.wml <?xml version='1.wmls#validatePASSWD($(passwd))"/> </do> </card> <card id="Results" title="Results:"> <p> You entered: $(passwd)<br/> </p> </card> </wml> The WMLScript code is shown in Sample 12. Try again!"). Sample 11: login. the application displays a new screen. The example we will now look at is a login application that asks the user to enter a password.0'?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.org/DTD/wml_1.1//EN" "http://www. Sample 12: login. If the user enters the right password.wapforum. The WML code for this application is shown in Sample 11.1. If the user enteres an incorrect password. } else { Dialogs. a dialog box is displayed asking the user to try again.go("login.wml#Results").

Command. Sample 13: LoginMIDlet. form. TextField. 0).When you run this application on a WAP-enabled device.microedition.lcdui.addCommand(login). private Command login. If the user provides an incorrect password. importjavax. "".setCurrent(tbox). 10. login = new Command("GetIn". form. } public void validateUser(String password) { if (password.append(password). 2).PASSWORD). } public void startApp() { display = Display.*. display.getDisplay(this). "You entered: "+password.midlet. public LoginMIDlet() { password = new TextField("Password:". form. form = new Form("Login").setCommandListener(this).OK. Figure 12a and 12b: correct password and incorrect password dialog This login application can be written as a MIDlet as shown in Sample 13. you see something similar to Figure 12b. } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(). private Form form.java import javax.equals("99")) { TextBoxtbox = new TextBox("Results:". display. private TextField password. . you see something similar to Figure 12a. public class LoginMIDlet extends MIDlet implements CommandListener { private Display display. 40.MIDlet.setCurrent(form).microedition.

Figure 13a: correct password Figure 13b: incorrect password As you might be able to figure out.equals("GetIn")) { validateUser(password. you may wish to associated icons with error or confirmation messages. display.ERROR). if(label.getString()).FOREVER). .getLabel(). Note: To give your application a professional look and feel. } } } If you run this application. error. Displayable d) { String label = c. This can be done in both WAP and MIDP. WAP and MIDP: what can they learn from each other? WAP and MIDP solve similar problems but each can learn a couple of things from the other. as applications get more sophisticated there won't be really much difference in the amount of code to be written. you will see something similar to Figure 13a (user provided correct password) and Figure 13b (user provided incorrect password). } } public void commandAction(Command c. AlertType. null. but it is worth noting that WAP supports wireless bitmap (WBMP) format and MIDP supports the non-patented Portable Network Graphics (PNG) format.setCurrent(error.} else { Alert error = new Alert ("Error: login incorrect". "Try again!".setTimeout(Alert. form).

The very nature of MIDlets (they exist on the device till they are explicitly removed) allow users to run them even when the server becomes unavailable (support for disconnected operations). and more importantly downloading MIDlets on real physical devices (such as the Motorola/Nextel i85s cellular phone) then you might be aware that MIDlets are not currently being provisioned over the air. Speaking of entertainment applications. Despite the fact that not all WAP-enabled devices support this feature. there is no standard interaction model defined for this element. The WML page can then be called from a WAP browser and the embedded MIDlet gets installed on the device. the application may run well on some devices and poorly on others. there are no comparable APIs available for this in MIDP. If you have any experience working with MIDlets. WML defines a SELECT element for providing a list (as shown in Samples 6 and 7). For example. ChoiceGroup. there is a need for some kind of an environment on the handset that allows the user to enter a URL for a MIDlet Suite. If a developer uses it. Integrating WAP and J2ME MIDP As mentioned earlier. This is important for entertainment applications (such as games) in a wireless environment. If the SMS contains a URL to a Java Descriptor (JAD) file specifying a MIDlet Suite then the recipient can install the application simply by confirming the SMS. In order to facilitate over the air provisioning. MIDlets. WAP and MIDP shouldn't be viewed as competing technologies but rather as complementing ones. Therefore. Another alternative approach for over the air provisioning is the use of Short Message Service (SMS) as have been done by Siemens where the installation of MIDlets is accomplished by sending a corresponding SMS. and others). on the other hand. I summarize the important features: y y y y WAP has support for additional phone functionality such as setup and integration with address book. List. MIDP provides a low-level graphics APIs that enable the programmer to have control over every pixel of the device's display.There are special features that are available in WAP but not in MIDP and vice versa. In addition to the MIDP high-level graphics APIs used in this article (such as Form. It is very likely that this will be possible in future version of MIDP. Here. The WML provides the tags and the possible presentation attributes. similar to Java Applets that are integrated in HTML. MIDP is the way to go for this. Some WAP-enabled devices interpret the SELECT tag as a popup menu list while others interpret it as a menu that can be used for navigation. but it doesn't define an interaction model. In order to enable this a WAP browser (with support for over the air provisioning) is needed on the device. MIDlets can be integrated into a WML page. . This environment could very well be a WAP browser. for example. Therefore. provide a clearly defined standard for interaction using commands.

1999) and Learning Wireless Java (O'Reilly & Associates. MIDP provides a rich set of APIs for constructing MIDlets and provides an excellent navigational and interaction model.Conclusion This article discusses the programming model of WAP and MIDP and shows how WAP applications can be rewritten as MIDlets. Mahmoud provides Java consulting and training services. Integrating WAP and MIDP opens up possibilities for new wireless applications and over the air distribution models. which suffers from low bandwidth and network disconnection. The examples shown throughout the article illustrate that any WAP application can be rewritten as a MIDlet or a MIDlet Suite. 2002). and is the author of Distributed Programming with Java (Manning Publications. MIDlets combine excellent online and offline capabilities that are useful for the wireless environment. . He has published dozens of articles on Java. For more information y y y y y y J2ME MIDP J2ME Wireless Toolkit WAP Forum Over the air provisioning Midletcentral (over the air provisioning using OTA server) About the Authors:Qusay H.

Sign up to vote on this title
UsefulNot useful