Pages

Wednesday, April 1, 2009

Gwt Rpc

Share it Please
Gwt Rpc
in this blog iam going to explain how to create a simple remote procedure call using gwt.The theme of the application is to get the reverse of the String that is sent from client.
so now let's create the files on the client side

1.ServletServiceInterface interface which extends RemoteService RemoteService is a marker interface . This says that this is Gwt Rpc .This is the synchronous definition and the server-side implementation must implement this interface.
public interface ServletServiceInterface extends RemoteService {
public String getTheStringReverse(String StringToServer);
}


2.ServletServiceInterfaceAsync interface .The asynchronous definition of our interface. It must have the same methods as the synchronous interface, except for the requirement that all of its methods must have an AsyncCallback object as a parameter and the methods may not return anything. The naming convention recommended for this interface is to suffix the name of our synchronous interface with the word Async.
In this we will define the same method as defined in ServletServiceInterface,But we will also define a argument called AsyncCallback which identifies as a callback method
public interface ServletServiceInterfaceAsync {
public void getTheStringReverse(String StringToServer,AsyncCallback callback);
}


now we will create classed for the Server side:

A class ServletServiceInterfaceImpl implementing the ServletServiceInterface interface and extending RemoteServiceServlet class. RemoteServiceServlet as the name suggests is similar to servlets httpServlet can be hosted on any servlet container
public class ServletServiceInterfaceImpl extends RemoteServiceServlet implements ServletServiceInterface {
private static final long serialVersionUID = 1L;
public String getTheStringReverse(String StringToServer) {
StringBuffer buffer=new StringBuffer(StringToServer);
return buffer.reverse().toString();

}

}

one important we need to do is to define the path of the rpc implementation class in gwt.xml file as


<servlet path= "/server/callback " class="com.example.server.ServletServiceInterfaceImpl"/ >

now from the onModuleLoad() , u can call as

private String StringToServer;
private ServletServiceInterfaceAsync asyncCallback;
private AsyncCallback callback ;
private HorizontalPanel hPanel;

public void onModuleLoad() {

asyncCallback=(ServletServiceInterfaceAsync)GWT.create(ServletServiceInterface.class);
((ServiceDefTarget)asyncCallback).setServiceEntryPoint(GWT.getModuleBaseURL()+"/server/callback");

callback =new AsyncCallback() {
public void onFailure(Throwable caught) {
caught.printStackTrace();
}
public void onSuccess(Object result) {

((TextBox)hPanel.getWidget(hPanel.getWidgetCount-2)).setText(result.toString());
}
};

hPanel=new HorizontalPanel();
hPanel.add(new TextBox());

hPanel.add(new Button("Get String Reverse",new ClickListener() {
public void onClick(Widget sender) {
StringToServer=((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).getText();
asyncCallback.getTheStringReverse(StringToServer, callback);
}
}));
RootPanel.get().add(hPanel);
}//OnModuleLoad () Close

}


No comments :

Post a Comment