A tutorial on Remote Procedure Calls (RPCs) in Google Web Toolkit

At some point in your GWT development, you’ll want to start communicating with a server. Handily, GWT has a lot of stuff built in to help you do this in as easy a way as possible. The GWT RPC framework is very helpful indeed for just this, and this post will show you how to create your RPC services easily.

The basics:

[ad#Adsence_inline_200*200]

GWT is divided into “client-side” and “server-side” interaction. “Client-side” is what’s done by the browser, and “server-side” is obviously what’s being done by the server. Server-side is what can’t or shouldn’t be done in the browser. It helps to think of the client-side as the frontend and the server-side as the backend, though this isn’t always the correct view to take.

GWT-RPC is essentially very easy client-server interaction. Create a few interfaces, edit an xml file, write a little code, done. Client-side code will be compiled by GWT into Javascript, and server-side code will be compiled into Java bytecode, meaning that you’re programming pure Java on the server-side, so you can use all kinds of native Java libraries that you couldn’t otherwise.

How to build a service into your GWT app:

This is a somewhat long one, so make yourself a cup (or seven cups) of coffee. Here’s the code for this tutorial, if you feel like just looking at code.

Make sure before you do this that you’re set up properly, with a blank project as detailed in this tutorial. However, the example that’s created by default makes use of RPCs, so if you just want a rough idea on how it works, have a look at that.

Alright, let’s get going.


First, let’s create the client-side interface to our service which lists all the RPC methods.

Right-click ie.flax.RPCTutorial.client (in my case) and select “New -> Interface”.

Call it something like “TestService”. “Service” at the end isn’t just a good naming convention, GWT is a little finicky with names. Services should end in “Service”, Asynchronous interfaces should end in ServiceAsync, the Server-side class should end in ServiceImpl.

Add com.google.gwt.user.client.rpc.RemoteService as an extended interface.

Now, you’ll see, there’s already an Error showing up in this interface. If you hover over the interface name, Eclipse will complain that it’s “Missing asynchronous interface TestServiceAsync”. Handily, Eclipse itself will fix this for you. If you place your cursor inside the name and press the Quick Fix hotkey (in my case, Command-1), it will come up with a variety of answers. We want the first one, because it’s easier than creating one yourself.

This has created an asynchronous interface for you. This contains the methods that you actually call from your code.

Next let’s create your method lists. We’ll simply get this to take in a number client-side, multiply it by two server-side, and log it, client-side. Easy-peasy. So, in TestService.java, throw in a method like so:

and in TestServiceAsync.java, a method like this:

Note that the Async version returns void and adds an AsyncCallback as an argument. This must be done for each method, depending on the type the method returns.

Lastly, add @RemoteServiceRelativePath(“test”) to TestService.java like so:

You can call this whatever you like, though be sure to edit web.xml appropriately. On the next page, server code.

About the Author

Carl Lange

I'm currently a Computer Games Development student at Carlow IT. I love programming and all things technical, and I'll learn anything if it's interesting. I'm passionate about technical education, and naturally about games. Check out my resume, and follow me on Twitter!

Visit Website

6 Comments

  1. kritic July 26, 2010

    came here from reddit..

    This site is really good.

  2. Nicolas July 28, 2010

    Honnestly, the google web site is very good source of GWT tutorials for beginner and even for many advanced aspect of GWT. And we can expect them to evolve as their framwork change too.

    For exemple, for your topic : http://code.google.com/intl/fr/webtoolkit/doc/latest/tutorial/RPC.html

    It would be better to blog about advenced topics, like debugging in Eclipse if you do not use jetty, how you can make your GWT code avaiblable from javascript…

    Or tell us in your experience, what is better using UiBinder or plain old GWT ? Maybe how you used the code splitter effectively and managed to download code while the user what doing other things…

Trackbacks for this post

  1. Blog added – Flax Project « GameDevBlogs says:

    […] Written by two Carlow I.T. Game Development students, this is another tech-centred blog. It currently focusses on bits and pieces of web development info, mixed in with some in depth coding tutorials. […]

  2. […] out the fact that the bubble sort algothrim doesn’t test the true power of  GWT which is asynchronous programming and reduced Latency. I do understand this and I personally think thats why Google Web Toolkit is […]