Java & J2EE Page 3 - Using RMI with Apache Jserv |
Enabling the servlet (Booklet.java) to talk to the remote Directory service is a fairly straightforward procedure. Just like any other RMI client, the servlet instantiates the remote object with a call to Naming.lookup(). After the remote object is instantiated, you can work with the object as if it were local. The init() and doGet() methods are the particularly interesting methods in the Booklet servlet. Upon initialization, the servlet first checks the SecurityManager and sets it if needed. Next, the servlet checks for the initialization parameter named "rmiRegistryHost", the name of the computer hosting the RMI registry to which the Directory service is bound. To specify the rmiRegistryHost, in your servlet zone's properties file, insert this line: If this arguement is not defined, the getInitParameter() method will return null and the rmiRegistryHost will be set to the localhost. Then the init() method obtains the Directory service with Naming.lookup(), and stores it in a private variable, keeping it around for reuse. If the remote service was obtained inside a service method, like doGet(), the servlet would have to obtain the remote object with every request, an expensive procedure where repeating it should be minimized. Instead, one instance of the remote directory service will be reused for the lifetime of the servlet. The doGet() method, then services the incoming requests to the servlet. It examines the parameters passed to it from the HTML form and then calls the appropriate methods on the remote Directory service. Nothing particularly special needs to be done in compiling the client classes; just use the basic java compiler, javac. Windows:
Make the JAR filesNow we're going to bundle our classes into JAR files. While not an absolute requirement, this eases the deployment step as well as distribution. At the command prompt, type: on Windows operating systems the slashes change to backslashes: The "c" tag tells the jar tool to create a new file. The "v" tag turns on verbose output, so you can see exactly what the jar tool is doing. The "f" tag tells the jar tool that you will specify the name of the jar file to be created, which is the argument following the "f" tag, client.jar. The next arguments specify which files should go into the jar file. On the client side, the only the stub class is absolutely necessary for RMI communication to work. Create the server jar file: Windows: On the server side, both stub and skeleton classes generated by rmic are necessary. Deploy the classes in JservSo far, the steps in this article have followed the procedures described in Sun's RMI tutorial or the Javadoc. The only really Jserv specific part is the deployment. Installation of the client servlet is fairly straight forward:
Now restart your Apache Jserv server to ensure that the client.jar file is accessible and the configuration changes take effect. Typically, an "apachectl restart" will do it. Run it!Now that the client has been deployed, the all that's left to do is to run the demo. First fire up the server (making sure that the server.jar file is in your classpath...): Windows: The server should start, printing status messages to the console. The -Djava.security.policy=java.policy parameter tells the Java virtual machine to use the security rules specified in the java.policy file. The file tells the JVM to accept all socket connections on ports above 1024. This parameter is necessary otherwise other machines won't be able to connect to the rmiregistry. In a web browser, go to the url which maps to the client.Booklet servlet. For a typical setup, this might be http://yourhost/servlets/client.Booklet. If you want to abbreviate the URL & not refer to the servlet by it's package, in your zone.properties file, you could add: This creates an alias for the client.Booklet servlet; it's nick name is "booklet". Now you can access the Booklet servlet with a URL something like, http://yourhost/servlets/booklet, instead. The servlet will return a form to your browser which will allow you to interact (search, add entries, select an entry, get all entries) with the AddressBook through the HTML form. As you interact with the AddressBook via the servlet, status messages on what the AddressBook is doing should appear in the console in which it was started.
blog comments powered by Disqus |