gwt实现rpc

1:在客户端也即client,实现service接口,方法如下:

package com.jpleausre.gwt.logon.client;

import java.util.ArrayList;

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

public interface GwtService extends RemoteService{
public String user(String name,String password);
public User update(User us);
}

 

2:1:在客户端也即client,实现ServiceAsync接口,方法如下:

package com.jpleausre.gwt.logon.client;

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

public interface GwtServiceAsync {
public void user(String name,String password,AsyncCallback callback);
public void update(User us,AsyncCallback callback);
}

3:在服务器端实现ServiceImpl类,并实现一些接口,方法如下:

package com.jpleausre.gwt.logon;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.jpleausre.gwt.logon.client.GwtService;
import com.jpleausre.gwt.logon.client.User;

public class GwtServiceImpl extends RemoteServiceServlet implements GwtService{

 public String user(String name, String password) {
  // TODO Auto-generated method stub
  //显示用户名
  if(name!=null && password!=null && name.equals("aa") && password.equals("bb")){
   String ll = name;
   return ll;
  }else{
   return "failure";
  }
 }
 //传输对象
 public User update(User us){
   return us;
 }
 

}

 

4:在继承了EntryPoint接口的类中调用,方法如下:

 GwtServiceAsync async = GwtServiceAsyncCreator
  .getHelloServiceAsync();
  async.user(intousename.getText(), intopassword.getText(),new AsyncCallback() {

 public void onFailure(Throwable arg0) {
  Window.alert("test");
 }

 public void onSuccess(Object arg0) {
  if(arg0.toString()!=null&&arg0.toString().equals("failure")){
   Window.alert("用户名密码错误");
   intousename.setText("");
   intopassword.setText("");
  }else{
  v.setVisible(false);
  v2.setVisible(true);
  String s = (String)arg0;
  
  viewl.setText("欢迎您:"+s);
  }

 }
});

 

5:实现异步入口访问路径封装

 public static class GwtServiceAsyncCreator {
  public static GwtServiceAsync getHelloServiceAsync() {
   GwtServiceAsync async = (GwtServiceAsync) GWT
     .create(GwtService.class);
   ServiceDefTarget target = (ServiceDefTarget) async;
   target.setServiceEntryPoint(GWT.getModuleBaseURL() + "/demogwt");
   return async;
  }
 }

 

注意:"/demogwt"是在*.gwt.xml中配置如下代码 <servlet path='/demogwt' class='com.jpleausre.gwt.logon.GwtServiceImpl'/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值