One of the greatest advantages which Jserv brings to Apache is ability to leverage the large number of API's available to Java. Remote Method Invocation (RMI) delivers several significant benefits to the servlet solution. The primary benefit of using RMI with servlets is that it significantly expands the variety of datasources which Apache can serve to the browser. Furthermore, RMI's simplifies code on the client side of the RMI connection (the servlet), and also allows for load distribution.
One of the greatest advantages which Jserv brings to Apache is ability to leverage the large number of API's available to Java. Remote Method Invocation (RMI) delivers several significant benefits to the servlet solution. The primary benefit of using RMI with servlets is that it significantly expands the variety of datasources which Apache can serve to the browser. Furthermore, RMI's simplifies code on the client side of the RMI connection (the servlet), and also allows for load distribution.
For example, if you wanted a servlet to access a database through JDBC, you would start off with importing the java.sql package into your servlet. Then your servlet would have to manage handling the database connection, preparing and executing SQL statements, as well as managing the result sets. This adds a lot of complexity to your servlet, as not only does it have to handle logic & return appropriate responses to the browser, it is also bogged down managing communications with the database.
Servlet <== JDBC ==> Database
With RMI, an abstraction layer is added between the data
source and the servlet. This layer, the RMI Service (or server tier), provides an simplified interface to the client, handling all of the internals of communicating with the database.
So, instead of having to manage the database communications
in your servlet, RMI hides all the that from your servlet behind its remote interface.
With RMI, the server tier can be anything, as long as it implements the defined remote interface. The service could even be hosted on a different machine with a different operating system. The client will not care if the server tier stores its data in a database, LDAP server, or even text files. Your Linux Apache-Jserv box could be pulling data from an application server on a Solaris box.
Example
Using RMI with Apache Jserv is fairly straight-forward, and the procedure in this example pretty much follows the RMI examples which can be found in the JDK documentation or Sun's tutorial. The steps include:
Design the remote interface
Implement the remote interface
Compile the classes, create the stub and skeleton classes
Create the client program (in this case, the servlet)
Deploy
In the following example, we will create a simple email address book. The server will store it's data in a Hashtable. Of course, the data in the server will disappear when the server is stopped, but that's okay for this example's purposes. For a more robust server, the address book data could be stored in a database, LDAP server, or any other type of data storage. The servlet (client) will use the address book service through RMI.
There are three packages in our example. The first one, is the directory.* package. It holds the remote interface class, as well as the Entry class, a data structure representing an directory record. The server package will contain the implementation of the remote interface. The client package contains the servlet which talks to the server implementation via RMI.