Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JSP中使用RMI_Jsp_Rmi - Fatal编程技术网

在JSP中使用RMI

在JSP中使用RMI,jsp,rmi,Jsp,Rmi,我正在尝试将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 <

我正在尝试将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();
        }
%>


我从未在JSP中尝试过RMI(只是因为它在技术上没有完全意义——您基本上使用了错误的工具来完成这项工作),所以我无法根据经验回答这个问题,但我强烈怀疑这个问题与错误地将Java代码放在JSP文件中而不是放在普通Java类中有关,这使得反射更难创建代理。我建议将Java代码放在普通Java类(和接口)中,然后重试。使用一个简单的方法来测试它。JSP应该只用于生成HTML。您的设计毫无意义。只有托管服务器的页面已被访问,服务器才会出现。每次访问它时都会创建一个新的。第一次访问后,所有后续绑定都将失败。为什么JSP页面会承载RMI服务器对我来说是个谜。