1

我正在尝试将 RMI 与 JSP 一起使用。我的代码如下:

你好.jsp

<%@ page import="java.rmi.Remote" %>
<%@ page import="java.rmi.RemoteException" %>
<%!
public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}
%>

客户端.jsp

<%@ page import="java.rmi.registry.LocateRegistry" %>
<%@ page import="java.rmi.registry.Registry" %>
<%@ include file="Hello.jsp" %>
<%!
public static class Client {

    public static String get() {

        String host = "<server's ip>";
        String res = null;
        try {
            Registry registry = LocateRegistry.getRegistry(host);
            Hello stub = (Hello) registry.lookup("Hello");
            res = stub.sayHello();
        } catch (Exception e) {
            res = ("Client exception: " + e.toString());
            e.printStackTrace();
        }
        return res;
    }
}
%>
<%
out.println(Client.get());
%>

服务器.jsp

<%@ page import="java.rmi.registry.Registry" %>
<%@ page import="java.rmi.registry.LocateRegistry" %>
<%@ page import="java.rmi.RemoteException" %>
<%@ page import="java.rmi.server.UnicastRemoteObject" %>
<%@ include file="Hello.jsp" %>
<%!
public class Server implements Hello {

    public Server() {}

    public String sayHello() {
        return "Hello, world!";
    }

}
%>
<%
        try {
            Server obj = new Server();
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

            // Bind the remote object's stub in the registry
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Hello", stub);

            out.println("Server ready");
        } catch (Exception e) {
            out.println("Failed: "+e.toString());
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
%>

我在 ubuntu tomcat 服务器上运行它,我从服务器收到以下错误:

客户端异常:java.lang.ClassCastException:$Proxy27 无法转换为 org.apache.jsp.src.rmi.Client_jsp$Hello

我该如何解决?谢谢

4

0 回答 0