Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Communicating with Server using Google Web Toolkit RPC

Save for later
  • 5 min read
  • 19 Jan 2011

article-image

 

Google Web Toolkit 2 Application Development Cookbook


communicating-server-using-google-web-toolkit-rpc-img-0

Over 70 simple but incredibly effective practical recipes to develop web applications using GWT with JPA , MySQL and i Report

  • Create impressive, complex browser-based web applications with GWT 2
  • Learn the most effective ways to create reports with parameters, variables, and subreports using iReport
  • Create Swing-like web-based GUIs using the Ext GWT class library
  • Develop applications using browser quirks, Javascript,HTML scriplets from scratch
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible


 The Graphical User Interface (GUI) resides in the client side of the application. This article introduces the communication between the server and the client, where the client (GUI) will send a request to the server, and the server will respond accordingly. In GWT, the interaction between the server and the client is made through the RPC mechanism. RPC stands for Remote Procedure Call. The concept is that there are some methods in the server side, which are called by the client at a remote location. The client calls the methods by passing the necessary arguments, and the server processes them, and then returns back the result to the client. GWT RPC allows the server and the client to pass Java objects back and forth.

RPC has the following steps:

  1. Defining the GWTService interface: Not all the methods of the server are called by the client. The methods which are called remotely by the client are defined in an interface, which is called GWTService.
  2. Defining the GWTServiceAsync interface: Based on the GWTService interface, another interface is defined, which is actually an asynchronous version of the GWTService interface. By calling the asynchronous method, the caller (the client) is not blocked until the method completes the operation.
  3. Implementing the GWTService interface: A class is created where the abstract method of the GWTService interface is overridden.
  4. Calling the methods: The client calls the remote method to get the server response.

Creating DTO classes


In this application, the server and the client will pass Java objects back and forth for the operation. For example, the BranchForm will request the server to persist a Branch object, where the Branch object is created and passed to server by the client, and the server persists the object in the server database. In another example, the client will pass the Branch ID (as an int), the server will find the particular Branch information, and then send the Branch object to the client to be displayed in the branch form. So, both the server and client need to send or receive Java objects. We have already created the JPA entity classes and the JPA controller classes to manage the entity using the Entity Manager. But the JPA class objects are not transferable over the network using the RPC. JPA classes will just be used by the server on the server side. For the client side (to send and receive objects), DTO classes are used. DTO stands for Data Transfer Object. DTO is simply a transfer object which encapsulates the business data and transfers it across the network.

Getting ready


Create a package com.packtpub.client.dto, and create all the DTO classes in this package.

How to do it...


The steps required to complete the task are as follows:

  1. Create a class BranchDTO that implements the Serializable interface:

    public class BranchDTO implements Serializable


    
    

  2. Declare the attributes. You can copy the attribute declaration from the entity classes. But in this case, do not include the annotations:

    private Integer branchId;
    private String name;
    private String location


    
    

  3. Define the constructors, as shown in the following code:

    public BranchDTO(Integer branchId, String name, String location)
    {
    this.branchId = branchId;
    this.name = name;
    this.location = location;
    }
    public BranchDTO(Integer branchId, String name)
    {
    this.branchId = branchId;
    this.name = name;
    }
    public BranchDTO(Integer branchId)
    {
    this.branchId = branchId;
    }
    public BranchDTO()
    {
    }


    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at £15.99/month. Cancel anytime
    
    

    To generate the constructors automatically in NetBeans, right-click on the code, select Insert Code | Constructor, and then click on Generate after selecting the attribute(s).

  4. Define the getter and setter:

    public Integer getBranchId()
    {
    return branchId;
    }
    public void setBranchId(Integer branchId)
    {
    this.branchId = branchId;
    }
    public String getLocation()
    {
    return location;
    }
    public void setLocation(String location)
    {
    this.location = location;
    }
    public String getName()
    {
    return name;
    }
    public void setName(String name)
    {
    this.name = name;
    }


    
    

    To generate the setter and getter automatically in NetBeans, right-click on the code, select Insert Code | Getter and Setter…, and then click on Generate after selecting the attribute(s).

Mapping entity classes and DTOs


In RPC, the client will send and receive DTOs, but the server needs pure JPA objects to be used by the Entity Manager. That's why, we need to transform from DTO to JPA entity class and vice versa. In this recipe, we will learn how to map the entity class and DTO.

Getting ready


Create the entity and DTO classes.

How to do it...

  1. Open the Branch entity class and define a constructor with a parameter of type BranchDTO. The constructor gets the properties from the DTO and sets them in its own properties:

    public Branch(BranchDTO branchDTO)
    {
    setBranchId(branchDTO.getBranchId());
    setName(branchDTO.getName());
    setLocation(branchDTO.getLocation());
    }


    
    

  2. This constructor will be used to create the Branch entity class object from the BranchDTO object.
  3. In the same way, the BranchDTO object is constructed from the entity class object, but in this case, the constructor is not defined. Instead, it is done where it is required to construct DTO from the entity class.

There's more...


Some third-party libraries are available for automatically mapping entity class and DTO, such as Dozer and Gilead. For details, you may visit http://dozer.sourceforge.net/ and http://noon.gilead.free.fr/gilead/.

Creating the GWT RPC Service


In this recipe, we are going to create the GWTService interface, which will contain an abstract method to add a Branch object to the database.

Getting ready


Create the Branch entity class and the DTO class.