这样做并不神秘。请记住,JSF 托管 bean 只是 Java 类,所以这里唯一的就是您的应用程序设计。为此,您可以拥有一个(非常)基本的骨架,如下所示:
在 JSF 项目中:
@ManagedBean
@RequestScoped
public class LoginBean {
private String user;
private String password;
public LoginBean() {
}
//getters and setters...
public String validateUser() {
UserService userService = new UserService();
if (userService.validateUser(user, password)) {
return "success";
}
return "problems";
}
}
public class UserService extends {
public UserService() {
}
public boolean validateUser(String user, String password) {
//do the Java RMI client job here...
boolean result = false;
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "UserServiceRMI";
Registry registry = LocateRegistry.getRegistry(args[0]);
UserServiceRMI userServiceRMI = (UserServiceRMI) registry.lookup(name);
result = userServiceRMI.validateUser(user, password);
} catch (Exception e) {
//you can (and must) do a better error handling
System.out.println("UserService exception:");
e.printStackTrace();
}
return result;
}
}
在 RMI 服务器项目中:
public interface UserRMIService extends java.rmi.Remote {
boolean validateUser(String user, String password) throws java.rmi.RemoteException;
}
public class UserService implements UserRMIService {
public UserService() {
super();
}
public boolean validateUser(String user, String password) {
//do the user validation here...
//check against the database or another way you want/need
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "UserRMIService";
UserRMIService engine = new UserService();
UserRMIService stub =
(UserRMIService) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(name, stub);
System.out.println("UserService bound");
} catch (Exception e) {
//you can (and must) do a better error handling
System.out.println("UserService exception:");
e.printStackTrace();
}
}
}