user510
user510

Reputation: 1

RMI server won't return class instances

I had a JSP/Servlet application that was running ok with MySQL. Now I had to implement the same application with RMI for my DAO's.

Through some tests I can see that it works fine when I return int, String or something like that. Remote methods with these type of objects works fine.

But when I try to call a remote method that returns an instance of a class I created (Produto) all the attributes of that object are null.

For example: I have a db table with 3 rows. The method returns a list with 3 objects on it, but if I call product.getName() for example I get this:

null null null

On the following code, I'm not even using any database methods. Just a test method to see that an object produto will be returned with null attributes.

My interface, ProdutoDAO:

public interface ProdutoDAO extends Remote{ 
    public Produto teste() throws RemoteException;
}

My (what would be my Server) ProdutoDAOImpl:

public class ProdutoDAOImpl implements ProdutoDAO {

    private Connection con;
    Produto prod =  new Produto();

    public ProdutoDAOImpl() throws RemoteException {
        con = Conecta.getConnection();
    }

    public Produto teste(){
        prod.setNome("Testing! THIS WILL RETURN AS NULL");      
        return prod;        
    }




    public static void main(String args[]) {        
        int port = 1099;

        try {
            ProdutoDAOImpl obj = new ProdutoDAOImpl();
            ProdutoDAO stub = (ProdutoDAO) UnicastRemoteObject.exportObject(obj, 0);

            Registry registry = LocateRegistry.createRegistry(port);
            registry.bind("ProdutoDAO", stub);

            System.err.println("Server ready");

         } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
         }       
     }
}

Index.jsp

Produto prod = new Produto();

try {
    Registry registry = LocateRegistry.getRegistry(null);
    ProdutoDAO produtoDAO = (ProdutoDAO) registry.lookup("ProdutoDAO");
    prod = produtoDAO.teste();

    %><%= prod.getNome() %><%

} catch (Exception e) {
    System.err.println("Client exception: " + e.toString());
    e.printStackTrace();
}

The problem is that <%= prod.getNome() %> is printing null.

Is it possible to return an object so I can read it's correct attributes?

Upvotes: 0

Views: 636

Answers (2)

user207421
user207421

Reputation: 311050

The class need either to implement Serializable or be an exported remote object. Yours is neither.

Upvotes: 1

user510
user510

Reputation: 1

Ok I figured it out.

Made my Produto class serializable and re-genrated the serial version on Eclipse.

public final class Produto implements Serializable {

     private static final long serialVersionUID = 8367908553994431734L;
     ...

}

Upvotes: 0

Related Questions