如何在jsp中使用RMI服务器

时间:2013-01-17 19:57:02

标签: java jsp rmi

我刚刚在java中实现了一个RMI服务器。它工作正常。现在我想使用JSP页面与我的服务器进行交互,但它无法正常工作。任何人都可以告诉我应该怎样做才能使它发挥作用?

MyServer.java

package pack1;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

public class MyServer {

private void startServer(){     
    try{
        Registry registry = LocateRegistry.createRegistry(2000);
        registry.rebind("MyData", new MyClassImpl());
    }catch(Exception ex){
        ex.printStackTrace();
    }
    System.out.println("Server is connected and ready to go....!!");
    System.out.println("Waiting for request...");
}
public static void main(String[] args){
    MyServer server = new MyServer();
    server.startServer();
}
}

MyClient.java

package pack1;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

class MyClient{

private void Test(){
    try{
        Registry registry = LocateRegistry.getRegistry(2000);
        MyClass myClass = (MyClass)registry.lookup("MyData");

        if(myClass.upload("AssadUllah","assad.xml")){
            System.out.println("Successfully uploaded file");
        }else{
            System.out.println("Problem uploading file");
        }

    }catch(Exception ex){
        ex.printStackTrace();
    }
}
public static void main(String[] args){
    MyClient client = new MyClient();
    client.Test();
}
}

MyClass.java

package pack1;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyClass extends Remote{
public Boolean upload(String fileName, String fileData)throws RemoteException;
//public String download(String fileName)throws RemoteException;
//public String checkNumber(int num)throws RemoteException;
//public int factorial(int num)throws RemoteException;
}

MyClassImpl.java

package pack1;

import java.io.*;
import java.util.regex.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyClassImpl extends UnicastRemoteObject implements MyClass{

private int c;

public MyClassImpl()throws RemoteException{
    c=0;
}
public Boolean upload(String fileName, String fileData)throws RemoteException{
    String postfix="";

try{

    Pattern pattern = Pattern.compile("[.][a-zA-Z]+");
    Matcher matcher = pattern.matcher(fileData);

    if(matcher.find()){
        postfix = fileData.substring(matcher.start(), matcher.end());
    }

    File inputFile = new File("E:\\UploadedFiles\\"+fileData);
        File outputFile = new File("E:\\DownloadedFiles\\"+fileName+postfix);

        FileInputStream IStream = new FileInputStream(inputFile);
        FileOutputStream OStream = new FileOutputStream(outputFile);

        while((c = IStream.read()) != -1){
            OStream.write(c);
        }
        IStream.close();
        OStream.close();

        return true;

    }catch(IOException ex){
        ex.printStackTrace();
    }
    return false;

}
}

我的jsp代码页.jsp

 <%@ page import="pack1.*"%>
 <%@ page import="java.rmi.registry.Registry"%>
 <%@ page import="java.rmi.registry.LocateRegistry"%>

 <%

 out.println(request.getRemoteAddr());

try{
Registry registry = LocateRegistry.getRegistry(2000);
MyClass myClass = (MyClass)registry.lookup("MyData");

String fileName = request.getParameter("fileName");
String myFile = request.getParameter("myFile");

myClass.upload(fileName, myFile);



}catch(Exception ex){
ex.printStackTrace();
}
%>

这是我用于在RMI服务器上传文件的JSP页面。代码在没有JSP的情况下运行良好但我希望它也可以与JSP一起使用。

0 个答案:

没有答案