19.6 Acting as an RMI Client

Java Servlet Programming, 2nd Edition > 19. Odds and Ends > 19.6 Acting as an RMI Client

 
< BACKCONTINUE >

19.6 Acting as an RMI Client

In Chapter 10, we saw how a servlet can act as an RMI server. Here we turn the tables and see a servlet acting as an RMI client. By taking the role of an RMI client, a servlet can leverage the services of other servers to accomplish its task, coordinate its efforts with other servers or servlets on those servers, and/or act as a proxy on behalf of applets that can't communicate with RMI servers themselves.

Example 19-7 shows DaytimeClientServlet, a servlet that gets the current time of day from the DaytimeServlet RMI server shown in Chapter 10.

Example 19-7. A Servlet as an RMI Client
import java.io.*; import java.rmi.*; import java.rmi.registry.*; import javax.servlet.*; import javax.servlet.http.*; public class DaytimeClientServlet extends HttpServlet {   DaytimeServer daytime;   // Returns a reference to a DaytimeServer or null if there was a problem.   protected DaytimeServer getDaytimeServer() {     // If you use RMI's dynamic code loading feature, you have to     // set a security manager such as RMISecurityManager     //if (System.getSecurityManager() == null) {     //  System.setSecurityManager(new RMISecurityManager());     //}     try {       Registry registry =         LocateRegistry.getRegistry(getRegistryHost(), getRegistryPort());       return (DaytimeServer)registry.lookup(getRegistryName());     }     catch (Exception e) {       log("Problem getting DaytimeServer reference", e);       return null;     }   }   private String getRegistryName() {     String name = getInitParameter("registryName");     return (name == null ? "DaytimeServlet" : name);   }   private String getRegistryHost() {     // Return either the hostname given by "registryHost" or     // if no name was given return null to imply localhost     return getInitParameter("registryHost");   }   private int getRegistryPort() {     try { return Integer.parseInt(getInitParameter("registryPort")); }     catch (NumberFormatException e) { return Registry.REGISTRY_PORT; }   }   public void doGet(HttpServletRequest req, HttpServletResponse res)                                throws ServletException, IOException {     res.setContentType("text/plain");     PrintWriter out = res.getWriter();     // Get a daytime object if we haven't before     if (daytime == null) {       daytime = getDaytimeServer();       if (daytime == null) {         // Couldn't get it, so report we're unavailable.         throw new UnavailableException("Could not locate daytime");       }     }     // Get and print the current time on the (possibly remote) daytime host     out.println(daytime.getDate().toString());   } }

This servlet should remind you of the applet you saw in Chapter 10. Both servlets and applets perform the same basic steps to access an RMI server. They both locate a registry using a hostname and port number, then use that registry to look up a reference to the remote object. The only possible difference is that a servlet, if it's taking advantage of RMI's dynamic code loading feature to automatically download the stub class file from another host, must first ensure it's running under the watch of a security manager to protect itself from the potentially hostile remotely loaded stub. An applet is guaranteed to run under an applet security manager, so this step isn't necessary. A servlet, however, can operate without a default security manager, so before acting as an RMI client it may need to assign one.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net