Tại client tạo interface thừa hưởng từ RemoteService và khai báo các phương thức cần làm (Tạm gọi là FirstInterface), ví dụ
package com.test.client;
import java.util.*;
import com.google.gwt.user.client.rpc.RemoteService;
public interface TodoListBackupService extends RemoteService {
void saveTodoList(String message);
String getTodoList();
}
Tại server viết một class (tạm gọi là ServiceClass) thừa hưởng từ RemoteServiceServlet và thi công FirstInterface vừa mới tạo ở trên và viết những phương thức đã định nghĩa trong FirstInterface. Ví dụ
package com.test.server;
import java.util.List;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.test.client.TodoListBackupService;
public class TodoListBackupServiceImpl extends RemoteServiceServlet implements
TodoListBackupService {
public void saveTodoList(String message) {
System.out.println("List were saved "+message);
}
public String getTodoList() {
// TODO Auto-generated method stub
return "MyHello";
}
}
Khai báo servlet vừa tạo trên server như một servlet bình thường (nhớ context path của nó để gọi trong client) hoặc khai báo trong file *.gwt.xml
package com.test.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface TodoListBackupServiceAsync {
void saveTodoList(String message,AsyncCallback callback);
void getTodoList(AsyncCallback callback);
}
Tại client gọi remote method như sau (từ in đậm là context path của servlet trên server) :
public void showMessage(){
TodoListBackupServiceAsync todoListBackupService = (TodoListBackupServiceAsync)GWT.create(TodoListBackupService.class);
ServiceDefTarget endpoint=(ServiceDefTarget)todoListBackupService;
endpoint.setServiceEntryPoint("/com.test.Hello/todoListBackupService");
AsyncCallback callback=new AsyncCallback(){
public void onSuccess(Object result){
Myresult = (String) result;
}
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
System.out.println("Error on loading result");
}
};
todoListBackupService.getTodoList(callback);
}