完整的代码,请参考附件。
服务器端:
客户端:
服务器端:
public class RmiUserServiceImpl extends UnicastRemoteObject implements
RmiUserService {
UserService service = new UserServiceImpl();
public RmiUserServiceImpl() throws RemoteException {
}
private static final long serialVersionUID = -7138363701504022490L;
public void create(String username, String password) throws RemoteException {
service.create(username, password);
}
public User login(String username, String password) throws RemoteException {
return service.login(username, password);
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://localhost:1099/UserService", new RmiUserServiceImpl());
System.out.print("Service start");
}
}
客户端:
public class Client {
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
RmiUserService service = (RmiUserService) Naming
.lookup("rmi://localhost:1099/UserService");
//service.create("new_user_01", "new_user_01");
System.out.println(service.login("new_user_01", "new_password_01"));
}
}