Building and Deploying the Web Service
The last steps to providing this Web service is to build the .war
file and deploy it to the server. As part of the build process, you will use the second of two tools, wsdeploy
tool reads a .war
file and the jaxrpc-ri.xml
file and subsequently generates another WAR file that is ready for deployment. Run the following Ant directives to build your war file (and have wsdeploy rebuild the file) and deploy the war file to the Tomcat server. In the Ant script I have provided, the deploy task will also start Tomcat with the new web service running in it.
Figure 2. Web Service Build and Deploy Success: If the compile, build, and deploy process are successful, you should see the status of your Insurance Quote Web service via your browser when you request the appropriate address.
Figure 3. The Entry Form: The GetQuoteMIDlet, on start up, displays the EntryForm in order to get client information.
To test whether your service has been properly built and deployed, you should be able to open a browser and enter the following http://localhost:8080/quoteservice/quoteservice in the address field and see something similar to what is shown in Figure 2 display in the browser window.
J2ME Web Service Client
Whew! With the Web service in place, you can now focus on the J2ME application that will call on the Web service. For this, you will need Sun's J2ME Wireless Toolkit, as it offers an important tool to create the necessary client side stubs and test your J2ME application's communications with the Web service through device emulators.
Open the Wireless Toolkit and request to Open Project. If you have placed the code provided into the apps directory of the Wireless Toolkit's root directory, then InsuranceQuote should be one of the projects listed. Open the InsuranceQuote project.
Generating J2ME Client Side Stubs
Remember the WSDL file you generated building the Web service? That file is going to be used by the Wireless Toolkit to generate the client-side stub code used by your MIDlet to access the Web service. This stub is a class that is a local Java object that acts as a proxy for the Web service instance. Your MIDlet makes a local method call to the stub and the stub, in turn, calls the Web service on the server.
Figure 4. The PolicyList: After getting and parsing the policy quote list XML document from the Web service, the GetQuoteMIDlet allows the user to select a policy for display through the PolicyList screen.
Figure 5. Policy Data Displayed in an Alert: After selecting a policy form the PolicyList screen, the policy data is displayed in an Alert screen.
Open the WSDL file with your favorite text or XML editor. It should be in your [WTK22]\apps\InsuranceQuote\server\WEB-INF\classes directory. The wscompile tool generated the WSDL and left you with one job. You must change the last line of the WSDL file to specify the actual address location of your service.
With that change, select the Project>Stub Generator option on the Wireless Toolkit tool bar. In the window that displays, select the quoteservice.wsdl
file you just edited and enter com.roaminginsurer.quoteservice
as the package for the stub code that is generated. If you take a look in the ./InsuranceQuote/src/com/roaminginsurer/quoteservice
directory, you should now see a set of Java files that define the stub and associated objects needed to communicate with the Web service.
|Figure 6. Navigating the GetQuoteMIDlet Screens: When the GetQuoteMIDlet launches, it displays the EntryForm. After entering in client information and hitting the GetQuote command, the list of policy quotes is displayed in the PolicyList. Finally, the policy quote data for the policy selected is displayed in an Alert screen.|
The MIDlet User Interface
Now you have a Web service and the client-side classes necessary to communicate with the Web service. On to developing the MIDlet application!
The MIDlet provided as part of this sample application uses a J2ME MIDP Form and a List screen (both from the javax.microedition.lcdui package) to drive the user interface. The MIDlet uses an instance of the form EntryForm to collect client information that the MIDlet will pass to the Web service (see Listing 3). The EntryForm as seen on an emulator screen is shown in Figure 3.
The MIDlet uses a List called PolicyList (see Listing 4) to display the policies returned by the Web service (see Figure 4).
When the insurance agent selects one of the policy quotes from PolicyList, an instance of javax.microedition.lcdui.Alert is used to display the policy information (see Figure 5). The entire MIDlet user interface navigation is modeled in Figure 6.