Connecting MIDP Applications to Web Services
This tutorial is the third in series designed to get developers started with MIDP and CLDC development. It is strongly recommended
that you complete the MIDP Quick Start Guide before beginning this tutorial. We continue building
on the "MyHello" project completed in that tutorial and show you how to connect it to a web service.
Contents
Requirements
You must have JDK 5 or better and the NetBeans IDE Mobility edition (download) before you can develop MIDP applications. For help getting your system set up, please see the installation instructions.
This tutorial also requires the Java EE/Web Edition for Web Services support available in the Full edition of the IDE. If you only installed the Mobility Edition, you must download the following plugins from the Update center:
- Web Applications
- Web Services
- GlassFish
- Mobility End to End
To intall the plugins do the folowing:
- In the IDE's main menu go to Tools > Plugins.
- In the Plugins dialog click the Available Plugins tab.
- In the Name column of the Available Plugins, find and check the box next to the plugins listed above.
- The NetBeans IDE Installer dialog shows the plugins about to be installed. If additional plugins are necessary, the IDE selects them automatically. Click Next.
- A license agreement may appear next. To enable End to End and Web Services functionality, accept the license agreement and click Install.
Connencting Mobile Applications to Web Services
The IDE enables you to create client MIDlets that can connect to web services
either through a direct two-tier architecture, or a three-tier
architecture that connects to web services through a web application.
The IDE provides two wizards for creating the connecting classes:
- Use the The Java ME Web Service Client wizard if you want to use the JSR-172
Web Services specification to create a two-tier connection to
web services. This wizard creates stub (client-side proxy) files and, optionally,
a sample client MIDlet that calls a web service.
- Use the The Mobile Client to Web Application Generator to create a three-tier
connection to web services if your target devices do not support the JSR-172 Web
Services specification. The wizard generates a servlet that connects to a
web application that includes a web service client.
Creating a Java ME Web Services (JSR-172) Client
- In the New File wizard (CTRL+N), select the project you want to create a web service client for, in this tutorial we are using the "MyHello" project.
- Choose MIDP under Categories. Under File Types, choose Java ME Web Service Client. Click Next.

- Identify the WSDL file location for the web service to be added to the project.
If the WSDL service is on the web:
- Choose Running Web Service as your source.
- Enter an URL for the WSDL file and enter a local filename for the retrieved WSDL.

- If you are behind a firewall, click Proxy Settings to designate the appropriate HTTP host and port.
- Click Retrieve WSDL to get a local copy of the WDSL file. The IDE downloads the WSDL file.

If you have a WSDL file in your environment:
- Select the WSDL file from your system and enter a filename.
- The IDE retrieves the WSDL file and the rest of the fields on the page are filled
with default names taken from the WSDL file name.
- Press Finish.
The IDE creates and opens a
.wsclient file. In the Main Window, you can see a list of the Exported Services, Client Information, and Client Options you can choose.

- In some rare situations, you might need to convert CLDC 1.1 types to CLDC
1.0 types, which means Float and Double types are converted to String variables.
To make the conversion, select the checkbox “Convert floating point
to String” on the settings file and click the Generate Stubs button.
- If you are behind a firewall, there is no public setting available for the
HTTPS proxy within NetBeans. You can change the NetBeans proxy settings by
choosing Tools>Options from the main menu. These settings do not, however,
propagate to emulators. You can set the proxy for the Wireless Toolkit by
choosing Tools > Java Platforms. Under Platforms, choose the Wireelss Toolkit,
and then select the Tools & Extensions tab. Click the Open Preferences
button and then select the Network Configuration tab. Changing the proxy for
other emulators is similar, but may differ from vendor to vendor.
- The generated files should not be edited. Any changes made to the files
are lost when you regenerate them.
Using the Mobile Client to Web Application Generator
Before you use the Mobile
Client to Web Application Generator, you must have a web project that includes
a web service client.
- Open the Mobile Client to Web Application Generator by selecting a Mobile
Project from the Projects view and choosing File > New File. In Categories,
chose MIDP. In File Types, choose Mobile Client to Web Application. Click
Next.

- In the Servlet and Client Type Selection page, select a web project,
servlet name, and package for the generated server code.
- In the same page, choose the type of web application the MIDlet interacts
with:
- If the mobile client connects directly to a web application that
does not use a web service, choose Methods in Web Application.
- If the mobile client connects to a web service through a middleware
web application, choose Web Services in Web Application, then use
the drop-down menu to choose the Web Service client.
- In the Service Selection page, identify the services to be exported
from the server to the client application.
- In the Client Options page, select a name and package for the generated
client class.
- In the same page, you can also determine the types of code to be generated:
- Stub methods for each exported service
- Code enabling multiple calls per session
- Code that allows floating point values
- Tracing code
- A sample MIDlet you can examine and modify
The template creates:
- a Java ME client class
- a servlet and supporting classes
- a mapping file in xml format
- if you wish, a MIDlet you can examine and modify
The Java ME client, mapping file and the MIDlet are generated as part of the
Java ME project. The servlet and supporting classes are generated in the web project. The servlet is also added to the web application, web.xml.
Summary
We have shown you two methods for connecting mobile applications to web services in this tutorial. Which one is best for your project depends on the technologies supported by your target devices. Fortunately, most handsets with Java support should be able to connect their applications to a web service using one of the two methods shown in this tutorial.
See Also
For more information about using the MIDP and CLDC development using NetBeans IDE pack see the following
pages: