6. Create the client in a new file named NumsClient.java in the com.test.MyFirstRPC.client package that extends the EntryPoint class.
public class NumsClient implements EntryPoint
{
}
7. Add an onModuleLoad() method to this new class, and create a text box.
public void onModuleLoad()
final TextBox IsEvenTxt = new TextBox();
8. Instantiate the NumsService and store it in a variable in the onModuleLoad()method.
final NumsServiceAsync NumsService = (NumsServiceAsync) GWT
GWT.create(NumsService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) NumsService;
endpoint.setServiceEntryPoint(GWT.getModuleBaseURL()+"Nums");
9. Create a new button, and add an event handler to listen for clicks on the button. In the handler, invoke the NumsService using the text typed into the text box as the input parameter to the service. Display the result in an alert dialog.
final Button checkNum=new Button("Is this an Even number?",
new ClickListener())
public void onClick(Widget sender)
AsyncCallback callback = new AsyncCallback()
public void onSuccess(Object result)
if(((Boolean) result).booleanValue())
Window.alert("Yes, "+ IsEvenTxt.getText()
+ "' is a Even number.");
else
Window.alert("No, "+ IsEvenTxt.getText()
+ "' is not a Even number.");
public void onFailure(Throwable caught)
Window.alert("Error while calling the Nums
Service.");
};
NumsService.isIsEvenTxt(Integer
parseInt(IsEvenTxt.getText()), callback);
});
10. Add the following entry to the application's module.xml file in order for the client to find this service.
<servlet path="/Nums" class=
"com.test.MyFirstRPC.server.NumsServiceImpl"/>
When you execute the app, you will get the result like this
Conclusion:
Google Web Toolkit has made RPC really simple and at the same time with the implementation of Asynchronous Calls the transactions occurs parallely and dynamically giving the User a complete AJAX experience and the developer a easy life to architect the application.