Sunday, September 5, 2010

Google Web Toolkit RPC

Google Web Toolkit
  • GWT Java-to-JavaScript Compiler
  • JRE emulation library
  • GWT web UI
  • Development tools (Hosted web browser, shell,..)
  • RPC

Communicating with the server
  • RPC
  • XML
  • JSON

Packages

 


Making GWT Remote Procedure Calls

 



Modul

 



UI

package cz.kibo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.TextBox;

public class SayHelloEntryPoint implements EntryPoint {

    private TextBox name = new TextBox();  
    private Label response = new Label();
    private Button button = new Button("Send");

    private HelloServiceAsync service;
    private AsyncCallback serviceCallback = new AsyncCallback() {
           public void onSuccess(Object result) {
              String string = (String) result;               
              response.setText(string);
           }

           public void onFailure(Throwable caught) {
               response.setText("There was an error: " + caught.toString());
           }
    };

    /**
    * Creates a new instance of MainEntryPoint
    */
    public SayHelloEntryPoint() {
    }

    /**
    * The entry point method, called automatically by loading a module
    * that declares an implementing class as an entry-point
    */
    public void onModuleLoad() {
     
       //Obtain Async service for interface
       service = GWT.create(HelloService.class);

       //go to URL
       //or use @RemoteServiceRelativePath("hello")
       ServiceDefTarget endpoint = (ServiceDefTarget) service;
       endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "hello");


       RootPanel root = RootPanel.get("content");       
       root.add(new Label("Name"));
       root.add(name);     

       // Listen for mouse events on the Add button.
       button.addClickHandler(new ClickHandler() {
             public void onClick(ClickEvent event) {
                   service.sayHello(new Person(name.getText()), serviceCallback);
             }
       });

       root.add(button);
       root.add(response);

    }
}


Service
  • extends RemoteService
package cz.kibo.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

//@RemoteServiceRelativePath("hello")
public interface HelloService extends RemoteService
{
    String sayHello(Person p);

}

AsyncService
  • return void
  • have last param AsyncCallback
package cz.kibo.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface HelloServiceAsync {
    void sayHello(Person p, AsyncCallback callback);
}

Service implementation

  • can extends RemoteServiceServlet
  • or you have to implements HelloServlet yet
package cz.kibo.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import cz.kibo.client.HelloService;
import cz.kibo.client.Person;

public class HelloServiceImpl extends RemoteServiceServlet implements HelloService{

    public String sayHello(Person p) {
       return "Hello " + p.name;
    }
}

RemoteServiceServlet
  • getThreadLocalRequest() 
  • getThreadLocalResponse()
  • onBeforeRequestDeserialized (String) 
  • onAfterResponseSerialized (String) 
  • shouldCompressResponse (HttpServletRequest, HttpServletResponse, String)
  • processCall(String)



Model
  • implements Serializable or IsSerializable
package cz.kibo.client;

import java.io.Serializable;

public class Person implements Serializable{

    public String name;
  
    public Person(){
              this(null);
    }

    public Person(String name) {
          super();
          this.name = name;      
    }
}

Netbeans project

No comments:

Post a Comment