Skip to main content

GWT - Calling remote procedures

Today's browsers include a special JavaScript object called XMLHttpRequest that allows communication between the browser and server without forcing a page refresh . This special JavaScript object is the basis for making browser-based Remote Procedure Calls(RPCs).

GWT provides two tools that sit on top of the XMLHttpRequest object.  The first is the RequestBuilder class, which is essentially a wrapper around this object, although it's a bit more Java-like in its usage.  The second tool, GWT-RPC, is more elaborate and lets you send and receive real Java objects between the client and server.

RequestBuilder class lets you create a request to be submitted to the server, gives you the ability to fire off the request, and provides access to the results sent back from the server.

String url = "/service/search";
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = rb.sendRequest("term=GWT+in+Action",
new RequestCallback() {
public void onResponseReceived (Request req, Response res) {

// process here
public void onError (Request req, Throwable exception) {
// handle error here
catch (RequestException e) {
// handle exception here

You can also use JSONObject and populates it with several properties, e.g.

JSONObject obj = new JSONObject();
obj.put("author", new JSONString("Hanson and Tacy"));
obj.put("pages", new JSONNumber(600));
String serializedObj = obj.toString();

The GWT-RPC mechanism lets you send Java objects between the client and server with only a little additional work on both the client and server sides.  First, define a service interface that will be implemented by the server. e.g.
The Server side codes:

public class ServerStatusData implements IsSerializable
public String serverName;

public interface ServerStatusService extends RemoteService
ServerStatusData getStatusData ();

Next comes the implementation of the server, as follows,

public class ServerServiceImpl
extends RemoteServiceServlet
implements ServerStatusService
public ServerStatusData getStatusData () {

ServerStatusData result = new ServerStatusData();
result.serverName = ....
return result;

Third Step:
Registering your service with GWT by adding it to the project configuration file.
<inherits name="'/>
<entry-point class='...ServerStatus'/.
<servlet path="/server-status" class= "....ServerServiceImpl"/>

When your service defined, implemented, and configured on the server, the next step is to address the client-side issues and finally call the service from the client.
GWT does most of the work for you, however you need to create one last interface. The GWT compiler uses this interface when it generates the service proxy object. A proxy object is an object instance that forwards the requests to another target. In this case, you'll call a local method, and the proxy object is responsible for serializing the parameters, calling the remote  service, and handling the deserializatin of the return value. You don't write the code fro the proxy class; the GWT compiler handles this for you. In the client-side code, you create the proxy object by writing the following:

Here you call the static method create() of the class, passing it the class object of the remote service interface.
The proxy object returned implements two interfaces: one that you need to create(ServerStatusServiceAsync), and one supplied by GWT(ServiceDefTarget)

public interface ServerStatusServiceAsync {
void getStatusData (AsyncCallback callback);

Calling the remote server service:
step 1: creating the proxy object

ServerStatusServiceAsync serviceProxy =
(ServerStatusServiceAsync) GWT.create(ServerStatusService.class);

Step 2: casting the proxy object to ServiceDefTarget:

ServiceDefTarget target = (ServiceDefTarget) serviceProxy;
+ "server-status");

Or using annotation:
Step 1:

 * The client side stub for the RPC service.

public interface ServerStatusService extends RemoteService
ServerStatusData getStatusData ();

RemoteServiceRelativePath will associates a RemoteService with a relative path. This annotation will cause the client-side proxy to automatically invoke the ServiceDefTarget.setServiceEntryPoint(String) method with GWT.getModuleBaseURL() + value() as its argument. Subsequent calls to ServiceDefTarget.setServiceEntryPoint(String) will override this default path.

Step 2:

 * Create a remote service proxy to talk to the server-side Greeting service.
private final ServerStatusServiceAsync serviceProxy = GWT

Step 3: Create a callback object

AsyncCallback callback = new AsyncCallback() {
public void onFailure (Throwable caught) {
GWT.log("RPC error", caught);
public void onSuccess (Object result) {
GWT.log("RPC success", null);

Step 4: make the remote service call

the RemoteServiceServlet is where all the magic happens. This servlet receives the data from the server, which we already mentioned must be text due to the way the underlying XMLHttpRequest object works, the deserializes the text data into Java objects. On the return trip, the RemoteServiceServlet serializes the return value into text, which can then be sent back to the browser.


Popular posts from this blog

Stretch a row if data overflows in jasper reports

It is very common that some columns of the report need to stretch to show all the content in that column. But  if you just specify the property " stretch with overflow' to that column(we called text field in jasper report world) , it will just stretch that column and won't change other columns, so the row could be ridiculous. Haven't find the solution from internet yet. So I just review the properties in iReport one by one and find two useful properties(the bold highlighted in example below) which resolve the problems.   example:
<band height="20" splitType="Stretch"> <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true"> <reportElement stretchType="RelativeToTallestObject" mode="Opaque" x="192" y="0" width="183" height="20"/> <box leftPadding="2"> <pen lineWidth="0.25"/> …

JasperReports - Configuration Reference

Spring - Operations with jdbcTemplate

This class manages all the database communication and exception handling using a java.sql.Connection that is obtained from the provided DataSource. JdbcTemplate is a stateless and threadsafe class and you can safely instantiate a single instance to be used for each DAO.

Use of Callback Methods
JdbcTemplate is based on a template style of programming common to many other parts of Spring. Some method calls are handled entirely by the JdbcTemplate, while others require the calling class to provide callback methods that contain the implementation for parts of the JDBC workflow. This is another form of Inversion of Control. Your application code hands over the responsibility of managing the database access to the template class. The template class in turn calls back to your application code when it needs some detail processing filled in. These callback methods are allowed to throw a java.sql.SQLException, since the framework will be able to catch this exception and use its built-in excepti…