0

Hi I am new to gwt(java based gui). Here i am trying to make a GWT server call.

I want to save my MyBean into my Database.

Later on i have to update,delete etc..

What are the possibilities and how can I achieve this ??

RAS
  • 8,100
  • 16
  • 64
  • 86

1 Answers1

4

There are couple of possibilities to hit the database with GWT like RequestFactory,RPC.

Before getting started with server calls please go through,

  • GWT RPC(Which makes server calls Asynchronesly)

  • RequestFactory( Alternative to GWT-RPC for creating data-oriented services.)

After gone through the links here is a Example of How to make an RPC.

Coming to your MyBean CRUD operations,in short the simple RPC structure as below :

GWT Code <===> InterfaceAsync <===> Interface (Synchronous)<===> Server Code 

I am trying to explain with you elements it self .

The Synchronous Interface(central to the whole RPC):

import com.google.gwt.user.client.rpc.RemoteService;
public interface BeanProcessRPCInterface extends RemoteService
{
    public Mybean processMybeanRPC(Mybean  bean);
}

The ASynchronous Interface(Key part on Client):

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

public interface BeanProcessRPCInterfaceAsync
{
    public void processMybeanRPC (Mybean bean, AsyncCallback callback);
}

Here you go with the Service(Equals to servlet) which implements "BeanProcessRPCInterface"

public class BeanProcessRPCImpl extends 
                   RemoteServiceServlet implements BeanProcessRPCInterface
{
    private static final long serialVersionUID = 1L;

    public Mybean processMybeanRPC(Mybean  bean)
    {
       //Process your widget  here (CRUD operations)

    }

**you can override doget,doPost,doDelete....etc along with your methods 
}

Map the above class in your web.xml;

     <servlet>
    <servlet-name>beanrpc</servlet-name>
    <servlet-class>com.server.BeanProcessRPCImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>beanrpc</servlet-name>
    <url-pattern>/beanrpc</url-pattern>
  </servlet-mapping>

Finally in your GWT Code .Use service as below

Using in Code:

//register the service .

   private final BeanProcessRPCInterfaceAsync beanService = 
        GWT.create(BeanProcessRPCInterface.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint('beanrpc');

requesting server with callback

beanService.processMybeanRPC(mybean, callback);
 AsyncCallback callback = new AsyncCallback()
    {
        public void onFailure(Throwable caught)
        {
           //Do on fail
        }

        public void onSuccess(Object result)
        {
           //Process successfully done with result (result is which you
           // returned in impl class) .
        }
    };

P.S .Beware of package structures:

BeanProcessRPCInterfaceAsync ,BeanProcessRPCInterface should be in client* package

MyBean class should be in shared* package

BeanProcessRPCImpl should be in server* package

Good Luck.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307