我有一個運行MySQL的JSP/Servlet應用程序。現在,我必須爲我的DAO實施與RMI相同的應用程序。RMI服務器不會返回類實例
通過一些測試,我可以看到它工作正常,當我返回int
,String
或類似的東西。使用這些類型的對象的遠程方法正常工作。
但是,當我嘗試調用一個返回我創建的類的實例(Produto
)的遠程方法時,該對象的所有屬性都是null
。
例如:我有一個3行的數據庫表。該方法返回上有3個對象的列表,但如果我叫product.getName()
例如我得到這個:
空 空 空
在下面的代碼,我甚至不使用任何數據庫方法。只是一個測試方法,看到一個對象produto
將返回與null
屬性。
我的界面,ProdutoDAO:
public interface ProdutoDAO extends Remote{
public Produto teste() throws RemoteException;
}
我(這將是我的服務器)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();
}
的問題是,<%= prod.getNome() %>
是印刷null
。
是否有可能返回一個對象,以便我可以讀取它的正確屬性?