0

I have just implemented a RMI server in java. It works fine. Now i want to interact with my server using a JSP page and its not working. Can anybody tell me what should i do to make it work?

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;

}
}

my jsp code page.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();
}
%>

This one is my JSP page i am using to upload the file at RMI server. Code works pretty well without JSP but i want it to be worked with JSP also.

Rebbeca
  • 87
  • 12
  • 1
    Please show how you're trying to connect with your RMI server from your Servlet. Remember to not write scriptlets (Java code) in your JSP: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197). – Luiggi Mendoza Jan 17 '13 at 20:54
  • All of my java classes are in the package. Let me show you my code – Rebbeca Jan 17 '13 at 21:03
  • @LuiggiMendoza.... please checkout my code. Thanks – Rebbeca Jan 17 '13 at 21:06
  • Please read [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/q/2422468/1065197). BalusC (Java EE expert) explains in a very detailed way how to upload the files to your Java Application Server (Tomcat, JBoss, etc) using JSP and a Servlet. Note that in his answer, the `doPost` method is the main thing because it will handle the binary stream of the file to load, so you can *replace* that logic and send the binary data using your RMI client. – Luiggi Mendoza Jan 17 '13 at 21:38
  • What exactly is the point? You may as well put the upload code straight into the JSP page. You don't need RMI for this task at all. Not that I see the point of providing a client interface to copy files from one server location to another either. Are you sure this code really does what you want it to do? NB you certainly shouldn't be passing the file data around as a String. – user207421 Jan 18 '13 at 04:16

0 Answers0