Thursday 2 July 2009

Three Tier Web Service Tutorial

This tutorial describes how you can use NetBeans IDE 6.5.1 to create a mobile client application that consumes a web service that you have already created before. The mechanics of this tutorial are taken from the
End-to-End Web Service Tutorial: Mobile Dilbert Application
http://www.netbeans.org/kb/60/mobility/mobile-dilbert.html
but the canvas and graphics related elements are excluded. Instead of using the Gilbert web service I used CalcARSWS which I produced changing the addition operation to maultiplication at the standard Net Beans example named Calculator.

1. Step: Create a web application, and then create a web service in it according to the below info:
package ars;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
*
* @author Ali Riza SARAL
*/
@WebService()
public class CalcARSWS {
/**
* Web service operation
*/
@WebMethod(operationName = "multiply")
public int multiply(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
return i * j;
}
}

2. Step: Create a Web Service Client named ThreeTierARS App according to the info you may find at Dilbert example: You simply run the app in the first step, CalcARSApp and find the absolute URL. You enter this WSDL URL into the related location at the second step.

3. Step: You create a simple mobile application ThreeTierARSClient which has ThreeTierARSMIDlet.java in it. You also create a Mobile Client To Webapplicaiton similar to the Dilbert application. Different to the Dilbert application this ThreeTierARSApp calls multiply(2,2) method of the CalcARSWS web service that resides in the CalcARSApp… To achieve this you make the follwing changes in the ThreeTiersARSMIDlet.java file.
public SimpleCancellableTask getTask() {
if (task == null) {
// write pre-init user code here
task = new SimpleCancellableTask();
task.setExecutable(new org.netbeans.microedition.util.Executable() {
public void execute() throws Exception {
// write task-execution user code here

ThreeTierARSClient client = new ThreeTierARSClient();
try{
int multiply_returnValue = client.multiply(2,2);
textField.setString(String.valueOf(multiply_returnValue));
}
catch(java.io.IOException e){
System.out.println(e.toString());
}
}
});
// write post-init user code here
}


And;

public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getSplashScreen());
// write post-action user code here
textField=getTextField();
}

At one point somewhere here it gave the error message:
…Wrapper class ars.MultiplyResponse is not found. Have you run APT to generate them?

I clicked on the ThreeTierARSClient.wsclient and did ‘Generate’…
It still did not work. I shut down the netbeans and restarted it. I deployed
CalcARSApp and then ThreeTierARSApp and ran the ThreeTierARSClient.
It worked. If you are interested to see the source files they are available on demand
from arsaral( at ) yahoo.com.

Kind regards.
Note: Do not forget to deploy the apps that include the webservices you use before trying to make any references to them.