Personal tools

Netbeans:NetbeansGWTRPC

From Adapt

Revision as of 23:08, 11 September 2008 by Scsong (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

AJAX/RPC: Quick and Dirty

Read the GWT docs if you want more detail.

Create Service Interface

Goes in the client package, must extend RemoteService=

package edu.umiacs.gwt.hello.client;

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

public interface HelloWorldService extends RemoteService
{
    public String doService(ServiceRequest request);
    
}

Serialize Custom Types

Custom types must implement the IsSerializable marker. Class *MUST* have an *explicit* empty constructor.

package edu.umiacs.gwt.hello.client;

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

public class ServiceRequest implements IsSerializable
{
    private String greeting;
    private char punctuation;
        
    public ServiceRequest(){}
    
    public ServiceRequest(String greeting, char punctuation)
    {
        this.setGreeting(greeting);
        this.setPunctuation(punctuation);
    }

    public String getGreeting(){ return greeting; }
    public void setGreeting(String greeting) { this.greeting = greeting; }
    public char getPunctuation(){ return punctuation; }
    public void setPunctuation(char punctuation) { this.punctuation = punctuation; }
}

Create Async Service Interface

Define the exact same methods the non-async interface contains but have all methods return void and append an AsyncCallback parameter to the end of each method.

package edu.umiacs.gwt.hello.client;

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

public interface HelloWorldServiceAsync
{
    public void doService(ServiceRequest request, AsyncCallback ac);
}

Implement the Service

In the root package, create a package named service which will hold all server-side, non-translatable code. Create the implementation class in this package. Class must extend RemoteServiceServlet and implement the non-async service interface.

package edu.umiacs.gwt.hello.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import edu.umiacs.gwt.hello.client.HelloWorldService;
import edu.umiacs.gwt.hello.client.ServiceRequest;

public class HelloWorldServiceImpl extends RemoteServiceServlet 
        implements HelloWorldService
{

    public HelloWorldServiceImpl()
    {
        System.out.println("HelloWorldServiceImpl.<init>");
    }
    
    public String doService(ServiceRequest request)
    {
        return request.getGreeting() + " to the world" + 
                request.getPunctuation();
    }
    
}

Register the Servlet with the Shell

In the ModuleName.gwt.xml file, add a servlet element:

<module>
   <inherits name="com.google.gwt.user.User"/>      
   <entry-point class="edu.umiacs.gwt.hello.client.HelloWorldApp"/>      
   <servlet class="edu.umiacs.gwt.hello.server.HelloWorldServiceImpl" path="/hello-service"/>        
</module>

The path is relative to the context root.

Invoke the service

package edu.umiacs.gwt.hello.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class HelloWorldApp implements EntryPoint
{
    Label lblSayHello = new Label();
    
    public void onModuleLoad()
    {
        GWT.log("onModuleLoad()", null);
        
        Button btnAsk = new Button("Say something");
        btnAsk.addClickListener(new ClickListener()
        {
            public void onClick(Widget w)
            {
                lblSayHello.setText("...Asking Server...");
                retrieveMessageFromServer();
            }
        });
        
        VerticalPanel vp = new VerticalPanel();
        vp.add(btnAsk);
        vp.add(lblSayHello);
        
        RootPanel root = RootPanel.get("div1");
        root.add(vp);
    }
    
    private void retrieveMessageFromServer()
    {
        HelloWorldServiceAsync service = 
                (HelloWorldServiceAsync)GWT.create(HelloWorldService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget)service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "hello-service";
        endpoint.setServiceEntryPoint(moduleRelativeURL);

        AsyncCallback callback = new AsyncCallback() 
        {
            public void onSuccess(Object result) 
            {
                String message = (String)result;
                lblSayHello.setText(message);
            }

            public void onFailure(Throwable caught) 
            {
                lblSayHello.setText("Error: " + caught.toString());
            }
        };

        service.doService(new ServiceRequest("Hello", '.'), callback);
    }
}

Ready to Run

Build and run the project and it should be a slightly more verbose Hello World.

Deployment to a real Tomcat instance

Add the servlet to the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>hello-service</servlet-name>
        <servlet-class>edu.umiacs.gwt.hello.server.HelloWorldServiceImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello-service</servlet-name>
        <url-pattern>/hello-service</url-pattern>
    </servlet-mapping>    
</web-app>

Modify the build.xml file to include the class files and the required libraries:

<target name="-war-dist">
   <war destfile="dist/hello-world.war" webxml="src/edu/umiacs/gwt/hello/web.xml">
      <fileset dir="dist/hello-world"/>
      <lib dir="dist/lib">
         <exclude name='gwt-dev*.jar'/>
         <exclude name='gwt-user.jar'/>
      </lib>
      <classes dir="build/classes"/>            
   </war>        
</target>

Development jar is not necessary and user jar will fail to load because it contains javax.servlet.Servlet

For more information: GWT Tutorial 4