我正在尝试将RMI与JSP一起使用。我的代码如下:
的hello.jsp
<%@ page import="java.rmi.Remote" %>
<%@ page import="java.rmi.RemoteException" %>
<%!
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
%>
Client.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());
%>
Server.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
我该如何解决这个问题? 感谢