C H A P T E R 7 |
Coding the Currency Converter Wireless Application |
This section of the tutorial shows you how to modify the Currency Converter application you created in Chapter 4 so that it can operate as client/server application. The chapter takes you through the tasks necessary to build a Web Module, modify the Currency Converter application, create the connecting code using the J2ME Wireless Connection Wizard.
The sections in this chapter are:
For this tutorial, you need to mount the filesystem where your Currency Converter application is stored. If it is not mounted, follow these instructions.
To mount the Currency Converter filesystem:
1. Choose FileMount Filesystem.
This opens the Mounting wizard.
2. Choose Local directory and click Next.
3. In the Select Items to Mount page, navigate to the filesystem where you created Currency Converter.
This tutorial will use a filesystem in the default Java Studio Mobility user directory for a user named JavaCoder on the Windows platform:
c:\Documents and Settings\JavaCoder\jstudio_6me_user\examples.
The examples filesystem is mounted.
Next, create the Web Module that stores the conversion rates for the Currency Converter.
1. Right-click the examples filesystem and choose NewAll Templates.
2. In the Choose template page, select the Folder template. Click Next.
3. Name the new folder myserver. Click Finish.
4. Right-click on the myserver folder and choose NewAll Templates.
5. Expand the JSPs & Servlets node and select Web Module. Click Next.
6. In the Choose Target page, make sure the directory path points to the myserver folder. Click Finish.
The Web Module is created and the filesystems myserver and myserver: /WEB-INF/classes are shown in the Explorer window.
The Web Module requires additional classes to perform the functions it needs to perform.
7. Right-click the myserver: /WEB-INF/classes filesystem and choose NewAll Templates.
8. In the Choose template page, select the Folder template. Click Next.
9. Name the new folder myserver. Click Finish.
10. Right-click on the folder you just created and choose NewAll Templates.
11. Expand the Java Classes node and select the Java Class template. Click Next.
12. Name the class ExchangeRatesService. Click Finish.
The class is shown in the Explorer window as a subnode of server: WEB-INF/classes.
13. Double-click on ExchangeRatesService to display the code for the class in the Source Editor window. Replace the code so the final class file looks like the following:
This file now contains the exchange rates.
The next step is to create the MIDP class that will extend the Web services to the Currency Converter MIDlet. This is where you use the J2ME Wireless Connection wizard.
1. Right-click the myconverter package and choose NewJ2ME Wireless Connection Mapping.
This opens the J2ME Wireless Connection Mapping wizard.
2. In the New Object Name page, accept the default value. Click Next.
3. In the Client Target Location page, enter RatesUpdater for the generated J2ME class, and choose the examples/myconverter package. Click Next.
4. In the Servlet Target Location page, enter RatesUpdaterServlet for the generated servlet class and choose the myserver: /WEB-INF/classes filesystem. Click Next.
5. In the Code Generation Options, check the Generate Stub Methods, Enable Multiple Calls per Connection, and Supports CLDC 1.1 Data Types checkboxes. Click Next.
6. In the Select Exported Services page, expand ExchangeRatesServices so that you see the three services:
7. Select all three services and click the Add button.
The services are shown in the Selected Services pane.
The Summary page lists all the changes you have designated. Notice that it will also generate a mapping file named EndToEndMapping.xml. Click Finish.
A dialog appears that lists the files created. Another dialog appears asking you to confirm changes in the deployment descriptor.
9. Click Process All to confirm the changes, then click OK to dismiss the second dialog.
a. Open Converter.java and add the following two imports:
b. Add Runnable to the list of interfaces that the Converter class implements:
c. Add the method new command() into constructor after the super call:
d. Modify the commandAction() method to handle new command() by inserting the following code:
} else if (command.getCommandType() == Command.SCREEN) { Display.getDisplay(midlet).setCurrent(new Form("Please wait...")); new Thread(this).start(); } |
e. Add a new method implementing Runnable:
This completes the coding for the Currency Converter application.
Executing the MIDlet is the same procedure as before.
1. Right-click on the converter MIDlet suite and choose Execute.
The Execute function compiles the MIDlet application, if necessary, before it executes the application.
As with the first Currency Converter application, the MIDlet suite runs on the default DefaultColorPhone skin within the J2ME Wireless Toolkit 2.1 installation.
Now you're ready to test the application in the device emulator.
2. Select the currency you want to convert by clicking the up and down arrow keys on the Select button.
You can select Dollars, Euros, or Yen.
3. Enter the currency amount to convert by clicking the emulator's numeric keys.
The application makes the conversion calculations and displays the results.
4. Click the button underneath the word "Exit" to exit the application.
In this chapter, you used the J2ME Wireless Connector wizard to extend the services Currency Converter MIDlet into a mobile client application that accesses services on a web server.
Copyright © 2004, Sun Microsystems, Inc. All rights reserved.