Getting message from the server using RPC( Remote Procedure Call )
This examples describes the procedure of retrieving
message from the server via RPC. The steps involved in geting message from the
server are described below:-
Step1:-Right-click on the GWT project node which
you have created and choose New > Other. In the New File wizard, click on the Google Web Toolkit category shows a file template named GWT RPC
Service. Click Next.
Step2:- Fill the sub package box, the name of the package where the files that will be generated will be stored.
Step3:-Click Finish. The Projects window will automatically updates to reflect changes
Step4:- Create the following Classes.
GWTService.java:- This is the client-side definition of the service.
package org.yournamehere.client;
import com.google.gwt.user.client.rpc.RemoteService;
public interface GWTService extends RemoteService{
public String myMethod(String s);
}
|
|
GWTServiceAsync.java:-This is the client side
definition. It provides a callback object that enables the asynchronous communication between server and client.
package org.yournamehere.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface GWTServiceAsync {
public void myMethod(String s, AsyncCallback callback);
}
|
|
GWTServiceImpl.java:-This is the servlet that implements the interface.
it provides the functionality for retrieving a quote via RPC.
package org.yournamehere.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.yournamehere.client.GWTService;
public class GWTServiceImpl extends RemoteServiceServlet
implements
GWTService {
public String myMethod(String s) {
return "Server says: " + s;
}
}
|
|
GWTServiceUsageExample.java:-This is the
interface that will instantiates the service.
package org.yournamehere.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
public class GWTServiceUsageExample extends VerticalPanel {
private Label lblServerReply = new Label();
private TextBox txtUserInput = new TextBox();
private Button btnSend = new Button("Send to server");
public GWTServiceUsageExample() {
add(new Label("Input your text: "));
add(txtUserInput);
add(btnSend);
add(lblServerReply);
final AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result) {
lblServerReply.setText((String)result);
}
public void onFailure(Throwable caught) {
lblServerReply.setText("Communication failed");
}
};
btnSend.addClickListener(new ClickListener(){
public void onClick(Widget w) {
getService().myMethod(txtUserInput.getText(), callback);
}
});
}
public static GWTServiceAsync getService(){
GWTServiceAsync service =
(GWTServiceAsync) GWT.create(GWTService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) service;
String moduleRelativeURL = GWT.getModuleBaseURL()
+ "gwtservice";
endpoint.setServiceEntryPoint(moduleRelativeURL);
return service;
}
}
|
Step4:-Change the entry point of class MainEntryPoint.java.Modify the onModuleLoad() method of the class to the following :-
public void onModuleLoad() {
RootPanel.get().add(new GWTServiceUsageExample());
}
|
|
Step6:-In the Projects window, right-click the project node and choose Run.
Output of the program
Download source code