I was asked for a trivial RPC example for GWT by Plasante, and here it is.
At the root of the project is MyApplication.gwt.xml, which is the config file that defines any GWT project. I don't want to get into what each tag is used for, as I leave that to the GWT documentation. I do want to point out though that we defined a <servlet>, which is the server-side class for our RPC service.
<servlet path="/myService" class="org.hanson.gwt.server.MyServiceImpl"/>
Under the public folder is the HTML file that will be used primarily for testing in "hosted" mode. This is a special mode used for testing GWT applications.
<title>Wrapper HTML for MyApplication</title>
<meta name='gwt:module' content='org.hanson.gwt.MyApplication' />
Lets look at the interfaces first. The first one, which we called MyService is the interface that is implemented by the server-side code. This is a sample, so I created a very simple interface with only a single method.
public interface MyService extends RemoteService
public String myMethod (String s);
The second interface is used for the client-side code. The name of the interface MUST be the same as the server-side interface with "Async" appended. It must implement all of the methods in the server-side interface, but all of the methods MUST also take an additional parameter, which is an AsyncCallback object.
public interface MyServiceAsync
public void myMethod(String s, AsyncCallback callback);
Notice that both interfaces are idential except for that additional parameter, and return type. I have highlighted that second part because I usually just copy the code from the first interface to the second, and it is easy to miss this small point.
Now, this is how you use it from the client application. First you create an AsyncCallback object by using the GWT.create() method. Note that this method takes the server-side interface as an argument, and will return an object that uses the client-side interface.
The object returned from the create() call also implements the ServiceDefTarget interface. We cast it to this interface, and set the serviceEntryPoint, which is the URL of the server-side code. We defined this location in the project configuration file with the <servler> tag.
Next we need to create an object instance that implements the AsyncCallback interface. Here we create an anonymous class for this. Calling server-side code happens asynchronously, meaning the code won't wait for a response (i.e. block). That is why we need to create this object. This object will handle the result when it makes it's way back fo the browser. We need to write code to handle both success and error conditions.
The last step is to actually call the server-side code. We do this by using the MyServiceAsync instance that we created, passing it our argument(s) as well as the AsyncCallback object to handle the result.
* Entry point classes define
public class MyApplication implements EntryPoint
public void onModuleLoad ()
// define the service you want to call
MyServiceAsync svc = (MyServiceAsync) GWT.create(MyService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) svc;
// define a handler for what to do when the
// service returns a result
AsyncCallback callback = new AsyncCallback()
public void onSuccess (Object result)
public void onFailure (Throwable ex)
// execute the service
svc.myMethod("Do Stuff", callback);
Here is what the server-side code looks like. It just returns a simple String. The two things that are important to note is that the code does NOT live in the "client" package. It can not live there, the "client" package is for client-side only code.
Confusing? I hope not. Here is the short version:
- Server side code can go anywhere.
- Client side code must be in the "client" package (or sub-package).
public class MyServiceImpl extends RemoteServiceServlet implements MyService
public String myMethod (String s)
return "You sent us '" + s + "'";