Google Web Toolkit
by Bruce Perry
|
Pages: 1, 2, 3, 4, 5
Nifty Service Mechanism
Using RPC with an Ajax application eliminates the necessity to explicitly deal with XMLHttpRequest and associated server return values, because the GWT objects handle the plumbing for you.
Each service your application defines requires two Java interfaces and one Java class. To compile these classes, you have to make sure that the gwt-user.jar library is on your classpath (an Ant file entry takes care of that). The following code sample shows the Java interface that defines our service.
package com.parkerriver.gwt.testapp.client;
import com.google.gwt.user.client.rpc.RemoteService;
public interface ShowRespService extends RemoteService{
String displayResponse(String req);
}
The service interface is required to extend the GWT interface RemoteService. It defines a single method displayResponse().
You also have to define an interface that the client, or eventual downloaded JavaScript code, will use to call this service method. The GWT uses a callback design pattern that I will describe when I show the client code (see MyForm.java).
package com.parkerriver.gwt.testapp.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface ShowRespServiceAsync {
public void displayResponse(String s,
AsyncCallback callback);
}
The naming convention is part of making available a service with GWT; add the "Async" suffix to the end of the service interface name (ShowRespService). The purpose of the AsyncCallback object, which is part of the GWT API, is to handle the service response for the client. At any rate, its behavior will become clearer after you look at the code where it is used. Both of these object definitions are a part of the Java code that is used to generate the application's client-side JavaScript.
A Servlet By Any Other Name
Finally, you have to define a Java class that implements the remote service interface. This class will live on the server-side of your Ajax application.
package com.parkerriver.gwt.testapp.server;
import com.parkerriver.gwt.testapp.client.ShowRespService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Date;
public class ShowRespServiceImpl extends RemoteServiceServlet
implements ShowRespService {
public String displayResponse(String req) {
if(req.length() < 1) {
throw new IllegalArgumentException(
"Blank submissions from the client are invalid.");
}
StringBuffer buf = new StringBuffer("Your submission: ");
Date date = new Date();
String serverInfo = this.getServletContext().getServerInfo();
buf.append(req);
buf.append("\n");
buf.append("Server response: ");
buf.append(date.toString());
buf.append("\n");
buf.append(serverInfo);
return buf.toString();
}
}
This class must extend RemoteServiceServlet, a GWT API object that itself extends javax.servlet.http.HttpServlet. In other words, this class and the interface it implements has to be deployed in your servlet container.
The Steps
Now that the service is defined, let's step back for a minute to review the application's directory structure. Google Web Toolkit includes a command-line script called applicationCreator that will generate a skeletal project directory structure for you. After you have unzipped the GWT download, you can find applicationCreator in the top-level directory. I used the following command line to start out:
applicationCreator -out /Users/bruceperry/1gwt/secondapp/ com.parkerriver.gwt.testapp.client.MyForm
Figure 3 shows what the directory looks like.

Figure 3: A GWT and IntelliJ project directory
applicationCreator generates the ./src directory and the MyForm-compile and MyForm-shell scripts. My Ant file executes MyForm-compile; the other script launches host mode in the GWT scheme of things. The ./src directory includes nested directories to match your initial package name, as Figure 4 shows.

Figure 4: A Java package and module for a GWT application
The MyForm.gwt.xml file is a generated configuration file that GWT calls a "module." It specifies the Java class that represents the "entry point" for your application, similar to a Java class that contains a main() method.
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Specify the app entry point class. -->
<entry-point class='com.parkerriver.gwt.testapp.client.MyForm'/>
</module>
The other files or directories are artifacts of an IntelliJ Web application project, including ./classes, ./WEB-INF, and ./gwtproj.ipr, so you do not have to pay special attention to them.
In addition, the ./www directory does not appear (unless you create it yourself) until you run the GWT compiler that generates your application code. My project uses the Ant file gwtproj.xml, as well as the properties defined in gwtproj.properties. Before I show you the Ant build file, we'll take a look at the MyForm.java class that represents the entry point for the application.