Server Implementation

The server class extends UnicastRemoteObject in order to be exported and implements the ServerHandlerInterface.

Listing 12.2 ServerHandlerImpl.java


import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

// server class
public class ServerHandlerImpl extends
	UnicastRemoteObject 
	implements ServerHandlerInterface{

 
//constructor 
  public ServerHandlerImpl()
	throws RemoteException{
 	
	System.out.println("Creating server object");
  }

  //remote method called by the servlet
  public String servletTest(String name)
	throws RemoteException{

	String res = "Your name is: "+name+"\n"
	+"\n\n Returned by RMI Server";
	return  res;
  }

  //main method creates an instance of the remote object
  //and binds it with the registry service
  public static void main(String args[]){
	try{
  	  ServerHandlerImpl obj = new ServerHandlerImpl(); 
	  Registry reg = LocateRegistry.createRegistry(1099);
	  reg.rebind("Server",obj);
	 }catch(Exception ex){
	 	System.out.println(ex.getMessage());
	 }
	}
 }

Explanation

The servletTest() method gets the name of the user as its parameter and returns a small message. The main() method creates an instance of the remote object implementation and binds it with the registry service.

 

 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com