【IT168 技术文档】
1.1 实践描述
服务器提供个人信息资料库(HashMap),客户端通过姓名来进行调用查询,预期返回该个人属性集对象(Properties)。主要是介绍RMI的具体应用和远程接口函数值的返回。
1.2 实践过程
1.2.1 远程接口定义
public interface Personal extends Remote { //------------------------------------------------------------------------- //Get personal info. by name public Properties getPersonal(String __name) throws RemoteException;
1.2.2 定义服务端执类
public class PersonalImpl extends UnicastRemoteObject implements Personal { //Personal info. container private HashMap m_infoMap; //------------------------------------------------------------------------- public PersonalImpl() throws RemoteException { m_infoMap = new HashMap(100); //Append personal info. records addPersonal("Paul", "Male", "1980/11/20", "Travelling, Fishing, Programming"); …… } //------------------------------------------------------------------------- //Add personal info. to map container by name private void addPersonal(String __name, String __sex, String __birthday, String __hobby) { Properties props = new Properties(); //Set properties props.setProperty("name", __name); props.setProperty("sex", __sex); props.setProperty("birthday", __birthday); props.setProperty("hobby", __hobby); //Add to map m_infoMap.put(__name, props); } //------------------------------------------------------------------------- //Get personal info. by name public Properties getPersonal(String __name) { return ((Properties)m_infoMap.get(__name) ); }