Client Implementation

Listing 6.3 TDApplet.java

import java.applet.Applet; 
import java.awt.Graphics; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 
import java.rmi.registry.*;

//Client implementation
public class TDApplet extends Applet { 
   String message = "blank"; 

  //remote reference 
  TDInterface obj = null; 

  //init() is called by the browser or applet viewer 
  //to inform this applet that it has been 
  //loaded into the system. 
  public void init() { 
	try { 
	 
	//Get a reference to the remote 
	//object Registry for the specified host 
	//on the specified port.
	Registry reg = LocateRegistry.getRegistry(getCodeBase().getHost(),2000);
	 
	//get a remote reference
	obj = (TDInterface)reg.lookup("Server");
	message = obj.getDateAndTime(); 
	} catch (Exception e) { 
	   System.out.println("Error: " + 
	   e.getMessage()); 
	} 
} 

//Paint the applet. 
public void paint(Graphics g) { 
		
	//Draw the text given by the 
	//specified string, using the
	//graphics context(s) current 
	//font and color. 
	g.drawString(message, 25, 50); 
    } 
}

Explanation

The applet in this example remotely invokes the getDateAndTime() method. A security manager is not installed in the client code because applets use the security manager already installed in the client browser. If the client were an application rather than an applet, you would need to install a security manager in the client. The applet(s) init() method gets a reference to remote registry and remote object, and then it calls the getDateAndTime() method. The paint method of the applet draws the string returned by the server.

Here is the HTML code for the web page that references the TDApplet class:

Listing 6.4 TDClient.html

<HTML> 
<title>Dynamic loading</title> 
<center> <h1>Dynamic loading</h1> </center> 
<p> 
<applet code="TDApplet" 
codebase = "dynamic/"
width=500 height=120> 
</applet> 
</HTML>

For dynamic class loading there must be an HTTP web server running on the machine from which you want to download classes. The "codebase" attribute specifies a directory in which the applet(s) class files are stored. The "code" attribute specifies the name of the applet(s) class file.

 

 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com