最近在做一个android的程序,因为要用到客户端之间的通信,所以就看了一些,这里的Webservice是以前经常用的一种。主要是通过soap协议,其中我们主要要用到两个包一个是ksoap的支持包,一个是axis2的支持包。这里写了一个登陆的例子。
下面的是服务器端的例子这里是我用到的一些包,主要是为了让结构更加的清晰,分别将数据的访问,逻辑的控制,和给服务器提供接口放在了不同的包里。
这里操作就不搭建数据库,默认一个账号密码进行模拟登陆。
//**********服务器端**************************************
/**
*
*/
package org.lei.model;
/**
* @author renlei
* @date 2014-2-24 下午5:00:08
* 实体类
*/
public class User {
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
//************************************
//***************************************
package org.lei.daoimpl;
import org.lei.dao.UserDao;
/**
* @author renlei
* @date 2014-2-24 下午4:57:42
* 实现数据库的访问函数
*/
public class UserDaoImpl implements UserDao{
/* (non-Javadoc)
* @see org.lei.dao.UserDao#Login(java.lang.String, java.lang.String)
*/
public boolean Login(String account, String passwrod) {
// TODO Auto-generated method stub
if(account.equals("renlei")&&passwrod.equals("123456"))
return true;
return false;
}
}
//*******************
/**
*
*/
package org.lei.func;
import org.lei.daoimpl.UserDaoImpl;
/**
* @author renlei
* @date 2014-2-24 下午5:04:17
* 该类中可以放一些逻辑处理
*/
public class UserFunc {
/**
*
* @param account
* @param password
* @return
*/
public boolean Login(String account,String password){
return new UserDaoImpl().Login(account, password);
}
}
//*********************
/**
*
*/
package org.lei.forclient;
import org.lei.func.UserFunc;
/**
* @author renlei
* @date 2014-2-24 下午5:06:34
* 该类用于发布给客户端调用
*/
public class ForServer {
/**
*
* @param account
* @param password
* @return
*/
public boolean Login(String account,String password){
System.out.println(account.toString()+password);
return new UserFunc().Login(account, password);
}
}
//************************************客户端****************************
*
*/
package org.lei.client;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.soap.SOAPBinding.Use;
import org.lei.model.Soap;
import org.lei.model.User;
/**
* @author renlei
* @date 2014-2-24 下午5:49:58
*/
public class WebServiceTest {
public static void main(String []arsg){
User user = new User("renlei", "123456");
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("account", user.getAccount());
properties.put("password", user.getPassword());
Object resultObject = new Soap("ForServer", "Login", "http://forclient.lei.org").getResult(properties);
System.out.println(resultObject.toString());
if(Boolean.parseBoolean(resultObject.toString())==true){
System.out.println("登录成功");
}
else {
System.out.println("登录失败");
}
}
}