Sunday, January 6, 2008

GWT Proxy

Here is a nice way to write GWT Proxy to call the server services using the proxy design pattern:

The advantages of this proxy are:

  • The client call the service as it's a local call
  • The proxy hides the difference between calling the services in hosted and development modes.
  • It improves performance since the end point is set only once

First write this Client util class:

public class ClientUtil {
private static final String PRODUCTION_URL = "";

* Check if the code run in production or development mode
* @return true if we run in production mode (i.e. Ruler.html is called)
public static boolean isProduction() {
return GWT.isScript();

* Set endpoint for service which want to access the server
* @param service the service class
* @param name the name of the service
public static void setEndpointForService(Object service, String name) {
ServiceDefTarget endpoint = (ServiceDefTarget) service;
String moduleRelativeURL = getRelativeURL() + name;

public static String getRelativeURL() {
String moduleRelativeURL = null;

if (!isProduction) {
moduleRelativeURL = GWT.getModuleBaseURL();
} else {
moduleRelativeURL = PRODUCTION_URL;

return moduleRelativeURL;

Then write your proxy class, for example:

package com.mycompany.client.util;


public class MyProxy {

private static MyServiceAsync myService = (MyServiceAsync) GWT.create(MyService.class);

static {
// serviceName is the name which define in app.gwt.xml file
ClientUtil.setEndpointForService(myService, "serviceName");

public static void foo() {
MyCallback myCallback = new MyCallback();

finally to call the server use:;

You can read more at:

No comments:


RSS Feeds Submission Directory