Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Mobile Java Gets a Much-needed Boost from Sun's New Mobility Toolkit : Page 3

J2ME developers are accustomed to neglect, but the new NetBeans Mobility Pack 4.1 is among the best updates to the wireless development toolbox yet this decade.


advertisement
Server-side Integration (Wireless Connection Wizard)
The second big feature added to the 4.1 version of the Mobility Pack is the Wireless Connection Wizard. This tool helps generate wireless MIDlet clients of enterprise services. The Wizard automatically generates the MIDlet and client-side classes and a Servlet which delegates the client requests to the appropriate enterprise service(s). Again as another first generation tool, the Wireless Connection Wizard offers a great deal of promise. I was able to generate and modify a MIDlet that connected to a simple Servlet in minutes using this tool.

Figure 6. The Wireless Connection Wizard: To open the Wireless Connection Wizard, select MIDP under Categories and Wireless Connection Wizard in the File Type in the New File dialog box.

To see this new feature, use the NetBeans 4.1 IDE to create a Web or Enterprise Application. Select File—> New Project. . . and then select Web under "Categories" and "Web Application" under "Projects" in the proceeding New Project dialog box.



Now, in the project, create a Servlet or other enterprise service using the NetBeans tools. I used the New Servlet tool. In my project, I created a Servlet to accompany my Web service of the last article. This Servlet responds with the Average Insurance Rate for any given city passed to the Servlet as a parameter. The code for my Web Application Project is available in the download associated with this article. Find the CityRates.zip in the download and extract the contents of this zip file into your NetBeans Projects folder (C:\Documents and Settings\<user folder> by default on Windows XP systems). After doing so, you should be able to open the project called CityRates and the project should contain the InsuranceAvg servlet.

The Wireless Connection Wizard can automatically create a MIDlet to access any enterprise service as long as the class and methods adhere to the following conditions:

  • The class (in my case, the Servlet) containing the methods must have public access.
  • The service containing the methods must contain a public constructor that takes no parameters.
  • The method accessed (or "exported" method as it is typically referred to) in the service must have public access.
  • The method's parameter types and return types must be supported. Supported parameter types and return types are: boolean, boolean[],byte, byte[], short, short[], char, char[], int, int[], long, long[], java.lang.String, and java.lang.String[].


Figure 7. Identify the Web Application Projec: Specify the Web application containing the enterprise service that you want to create a MIDlet to access and specify what to call the delegate Servlet that will be created to act as the intermediary between the MIDlet client and the enterprise service.
 
Figure 8. Identify the Exported Services: Select from the available public "services" or methods those with which you want the MIDP client to communicate.

In the case of the InsuranceAvg servlet, a single "exported" method was added to the servlet called getAverageInsuranceRate(String city) as shown in the listing below. For the purposes of this demonstration, the average city insurance rate is artifically calculated by generating a random number between 0 and 4 and multiplying 100:

public String getAverageInsuranceRate(String city){ if (city!= null) { return city + " average Gold Insurance rate is: $" + new java.util.Random().nextInt(4) * 100; } else { return "No city specified."; } }

With the service (i.e. the Servlet) in place, the Wireless Connection Wizard can now be used to generate a MIDlet client. Create another project, this time a Mobile Application, in the NetBeans IDE. As the Wizard will generate the MIDlet for you, you do not need to create the HelloMIDlet as suggested in the New Project dialog box. In fact, just create an empty Mobile Application and the Wizard will do the rest in a moment.

After you have created the project, right click on the project and request a New—>File/Folder… from the corresponding menu.

Figure 9. Specify the MIDlet and Associated Client Code: The last step before the Wizard generates the necessary code is to specify the client side class name and indicate which parts should be generated.

From the ensuing dialog box, request a MIDP Wireless Connection Wizard as shown in Figure 6. The Wireless Connection Wizard will then guide you through the creation of your MIDP enterprise service client.

The Wireless Connection Wizard will identify the Web applications already in your NetBeans environment or you can request to open a project that is not already available. You must name the Servlet the Wizard will create that delegates the MIDP client requests to the enterprise services and place it in an appropriate package in your Web application project (see Figure 7).

Figure 10. The MIDlet in Communication with the Enterprise Service: The Get Average City Insurance MIDlet running and communicating with the City Rates enterprise service.

After identifying the Web application with the service to which you want to connect, you must select the services to "export" (see Figure 8). Again, the exported enterprise services or methods must be public and have parameters of supported types.

The last step in the Wireless Connection Wizard is to name and generate the client application (see Figure 9). The Wireless Connection Wizard generates all the client code, stub methods, and a sample MIDlet (Visual MIDlet—of course) based on the specifications you provide in this last step. When finished, the Wizard generates code to put in both the MIDP application and Web application projects. The MIDP project gets the client code, stub and MIDlet while the Web application project is provided the delegate servlet and supporting classes.

Using the Wireless Connection Wizard, I created a new client side project called GetAverageRate that contains the AvgCityRateClientMVD MIDlet. After the Wizard generated the MIDlet, the only work left for me was to use the Visual Designer to pretty-up the labels and display. The GetAverageRate project is located in the GetAverageRate.zip file in the download. If you extract the contents of this zip file into your NetBeans Projects folder (C:\Documents and Settings\<user folder> by default on Windows XP systems) you will have both ends of the MIDP client and Web application enterprise service example shown through this section of this article. When you have the resulting client running against the service, your MIDP client should look something like what is shown in the screens of Figure 10.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap