Search
Calendar
July 2017
S M T W T F S
« Sep    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
Your widget title
Archives

PostHeaderIcon Basic RPC call with GWT

Let’s assume you have a “Hello World” GWT application. You need emulate a basic RPC call (RMI, EJB, etc.). Here is the program:

Under the *.gwt.client folder:

Create an service interface:

@RemoteServiceRelativePath("fooService")
public interface FooService extends RemoteService {
    public String getHelloFoo(String totoName);
}

Create another interface for asynchronous call. You can notice the method name differs lightly from the one in the other interface:

public interface FooServiceAsync {
  void getHelloFoo(String fooName, AsyncCallback<String> callback);
}

Under the *.gwt.server folder, create an implementation for service interface:

public class FooServiceImpl extends RemoteServiceServlet implements FooService {
  public FooServiceImpl() {
   // TODO init
    }

  public String getHelloFoo(String fooName) {
  // TODO call actual service
    return "hello world!";
  }
}

In the web.xml file, add the following blocks:

	  <!-- Servlets -->
	<servlet>
		<servlet-name>fooService</servlet-name>
		<servlet-class>com.......server.FooServiceImpl</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>fooService</servlet-name>
		<url-pattern>/ivargwt/fooService</url-pattern>
	</servlet-mapping>

The tags content match the argument given as parameter to RemoteServiceRelativePath annotation above.

From then, in your concrete code, you can instantiate the service and call remote method:

FooServiceAsync fooService = GWT.create(FooService.class);
fooService.getHelloFoo("how are you?", new AsyncCallback<String>() {

				public void onSuccess(String result) {
					MessageBox.alert("OK", result, null);
				}

				public void onFailure(Throwable caught) {
					MessageBox.alert("ERROR", "rpc call error-" + caught.getLocalizedMessage(), null);
				}
			});

Now you can compile, package your war and deploy under Tomcat or WebLogic.

NB: special to “black-belt GWT guy” David Chau from SFEIR.

Leave a Reply