Home Segments Top Top Previous Next

1028: Mainline

Now that you have completed the definition of the server-side RatingServer class, you move back to the client side to modify the ClientStub class to access a remote—rather than local—instance of the RatingServer class.

Another class method, lookup, of the Naming class provides the required access using a host identifier, such as whitney.ai.mit.edu, and the name with which you have chosen to identify the remote instance.

As you did on the server side, you need a trycatch combination to deal with thrown exceptions.

import java.rmi.*;
import java.math.*;
public class ClientStub {
 public static void main(String args[]) {
  // Construct a movie for testing
  Movie movie = new Movie(2, 3, 8, "Psycho");
  // Construct a rating server for testing
  try {                                                              
   String computer = "whitney.ai.mit.edu";                           
   System.out.println("The client is asking server, "                
                      + computer + ", for a rating");                
   RatingServerInterface ratingServer                                
    = (RatingServerInterface)                                        
      (Naming.lookup("//" + computer +"/ratingService"));            
   int rating = ratingServer.serverRating(movie);                    
   System.out.println("The server, "                                 
                      + computer + ", returned " + rating);          
  }                                                                  
  catch (Exception e) {                                              
   System.err.println("Rating client exception: " + e.getMessage()); 
   e.printStackTrace();                                              
  }                                                                  
 } 
}