Home Tutorials Swt Getting message from the server using RPC( Remote Procedure Call )

Related Tutorials


Share on Google+Share on Google+

Getting message from the server using RPC( Remote Procedure Call )

getting message server

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.
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 

  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: "));

  final AsyncCallback callback = new AsyncCallback() {
  public void onSuccess(Object 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 = 
  ServiceDefTarget endpoint = (ServiceDefTargetservice;
  String moduleRelativeURL = GWT.getModuleBaseURL() 
  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



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: Getting message from the server using RPC( Remote Procedure Call )  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image