//*****************************************
//* 用rmi實現j2ee分佈試應用之試驗手記
//* 試驗人 :火火火
//* email :jun_bai@sohu.com
//* 試驗時間:2001/12/22 下午 于虎門大板地
//*****************************************
// 远程方法调用(rmi)机制可以把面向对象的思想进一步扩展,因为你可以调用
//的对象不仅可以在本机上,也可以在别的主机上。本文就简单介绍rmi的编程方法。
//下面就通过一个例子来说明编写的过程。
//
//*1.编写服务器的接口:这一步是最主要的部分,因为接口是连接客户机
//*与服务器的关键部分。在这个例子中,接口很简单,代码如下:rem.java
//************************************************************************
//*rem.java
//************
import java.rmi.*;
public interface rem extends remote
{
public string getmessage() throws remoteexception;
public string getauthor() throws remoteexception;
}
//************************************************************************
//*************************************
//*2.编写服务器对象:remserver.java
//************************************************************************
//*remserver.java
//****************
import java.rmi.*;
import java.net.*;
public class remserver
{
public static void main(string[] args)
{
try
{
remimpl localobject = new remimpl();
naming.rebind("rmi://localhost/rem",localobject);
}catch(remoteexception re)
{
system.out.println("remoteexception:"+re);
}
catch(malformedurlexception mfe)
{
system.out.println("malformedurlexception:"+mfe);
}
}
}
//************************************************************************
//*************************************
//*3.编写创建服务器对象的服务器程序:remimpl.java
//************************************************************************
//*remimpl.java
//************
import java.rmi.*;
import java.rmi.server.*;
public class remimpl extends unicastremoteobject implements rem
{
public remimpl() throws remoteexception
{
}
public string getmessage() throws remoteexception
{
return "here is a remote message.";
}
public string getauthor() throws remoteexception
{
return "fancy.";
}
}
//************************************************************************
//*************************************
//*4.编写客户端代码:remclient.java
//************************************************************************
//*remclient.java
//************
import java.rmi.* ;
import java.net.* ;
import java.io.* ;
public class remclient
{
public static void main(string[] args)
{
try
{
string host =(args.length>0)?args[0]:"192.1.1.23"; //改為你的rmi遠程務器的ip地址.
rem remobject=(rem)naming.lookup("rmi://"+ host +"/rem");
system.out.println(remobject.getmessage()+"<br>");
system.out.println(remobject.getauthor());
}
catch(remoteexception re)
{
system.out.println("remoteexception:" + re);
}
catch(notboundexception nbe)
{
system.out.println("notboundexception:" + nbe);
}
catch(malformedurlexception mfe)
{
system.out.println("malformedurlexception:" + mfe);
}
}
}
//************************************************************************
//*************************************
//*5.编写jsp客户端代码:rem.jsp
//************************************************************************
//*rem.jsp
//************
<%@ page import="java.rmi.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="rem" %>
<%
try{
string host ="192.1.1.23" ; //改為你的rmi遠程務器的ip地址.
rem remobject=(rem)naming.lookup("rmi://"+ host +"/rem");
out.println(remobject.getmessage()+"<br>");
out.println(remobject.getmessage());
}
catch(remoteexception re)
{
out.println("remoteexception:" + re);
}
catch(notboundexception nbe)
{
out.println("notboundexception:" + nbe);
}
catch(malformedurlexception mfe)
{
out.println("malformedurlexception:" + mfe);
}
%>
//************************************************************************
/*
//************************************************************************
步驟說明:(假設你的jdk安裝在c:\jdk1.3.0_01下)
1.編譯javac_remsverver.bat服務器端代码
c:\> c:\jdk1.3.0_01\bin\javac remserver.java
2.編譯remimpl.java编写创建服务器对象的服务器程序
c:\> c:\jdk1.3.0_01\bin\rmic remimpl
將會自動產生remimpl_skel.class,remimpl_stub.class兩個文件.
3.运行rmiregistry程序,启动注册系统,使得服务器可以注册在机器上,以供客户调用。
c:\> start c:\jdk1.3.0_01\bin\rmiregistry
4.編譯javac_remclient.bat客戶端代码
c:\> c:\jdk1.3.0_01\bin\javac remclient.java
5.运行服务器程序remserver.class
c:\> start c:\jdk1.3.0_01\bin\java remserver
6.运行客戶程序remclient.class
c:\> c:\jdk1.3.0_01\bin\java remclient
在dos可以看到結果:
here is a remote message.
fancy.
7.运行jsp客戶程序rem.jsp
要將rem.class,remimpl_stub.class兩個文件拷貝到rem.jsp客戶端的classpass(web-info)
下,確保可以被rem.jsp找到.
在ie可以看到結果:
here is a remote message.
fancy.
全文完.
參考書籍:
編程高手成長之路6—jsp高級編程 第4章2小節 ;
//************************************************************************
*/
