MCS-270 Lab 4: Three-Tier Development (Spring 2001)

Due: March 21, 2001

In this lab, you will extend your work from the previous lab into the three-tier architecture. All the instructions below assume you are working on one of the MCS department PCs running Linux.

First, try using a working version of the client applet, which is linked from the web version of this lab handout. You can also try a variant that connects to a dummy server (which gives a fixed answer regardless of your query). These two use the identical Java applet code, and the HTML files differ only in one line, which is

<param name="chainServer" value="max-chain">
in the first case and
<param name="chainServer" value="max-dummy-chain">
in the second. This illustrates how you can try out your server (dummy or real) with my client applet: only this one line will need changing. (More on this below, where it becomes relevant.)

You discovered above that one client can contact different servers, so long as they implement the same protocol. The converse is true as well: one server can be used by multiple clients. For example, in the interest of accessibility, we should have a textual client as well as the GUI applet. This is useful for testing as well. I have a (crude) textual client for you to try out, which can access any server: my real one, which uses a database; my dummy one, with fixed answers; or the ones you will write. To try out the textual client with my real server, do the following in a shell window (assuming you are running the csh or tcsh):

setenv CLASSPATH ~max/public_html/codebase
java edu.gac.max.movies.textual.FindActor \
 rmi://kilpinen.mcs.gac.edu/max-chain
To instead connect to my dummy server, you would simply replace max-chain by max-dummy-chain in the last line. (Note that the second line ends with a space and then a backslash, to allow the third line to serve as a continuation of what would otherwise be one very long line.)

The interface that defines the common protocol that any client can use to access any server is given by the following three files; you should look at them, since they serve as the basis for the code you will write:

Your first server should be another dummy server, like mine, just to make sure that you have the mechanics down of how to compile and deploy a server. (These mechanics are a bit messy. I strongly advise using the mouse to copy commands from the web version of this lab handout and paste them into your shell, rather than retyping.) I am providing you with the code for my dummy server; all you absolutely need to do is change the package line at the top of each file (to show these classes as being in some package of your own), change the RMI name from max-dummy-chain to one of your own in the call to Naming.rebind, compile, deploy, and test. However, I strongly urge you to also make some modification in the fixed response that is provided, so that when you test, you will be able to see for sure that it is really your own server that is being contacted. The two files are:

In the following instructions, I am going to pretend your username is jstudent; substitute your real one.
  1. Edit the two files so that the package line at the top indicates a package somewhere subordinate to edu.gac.jstudent rather than edu.gac.max. (I will assume you use edu.gac.jstudent.movies.dummy.)
  2. Further edit ChainImpl.java to rebind under the name jstudent-dummy-chain instead of max-dummy-chain.
  3. Further edit ChainImpl.java to offer your own favorite fixed choice of stores, and StoreImpl.java to offer your own favorite fixed movies. (These are to make your server recognizable.)
  4. You will need a public_html/codebase directory to hold your compiled classes, in order that the web server on kilpinen can distribute your remote-access classes to clients. (It is simplest to put the other class files there as well.) Create this directory as follows:
    mkdir ~/public_html
    mkdir ~/public_html/codebase
    
  5. Now compile your program as following
    setenv CLASSPATH ~max/public_html/codebase:$HOME/public_html/codebase
    javac -d ~/public_html/codebase StoreImpl.java
    rmic -d ~/public_html/codebase edu.gac.jstudent.movies.dummy.StoreImpl
    javac -d ~/public_html/codebase ChainImpl.java
    rmic -d ~/public_html/codebase edu.gac.jstudent.movies.dummy.ChainImpl
    
  6. Because your server makes use of the basic Chain, Store, and Movie classes I provided (the client/server interface), you need to copy my class files for these three classes into your public_html/codebase directory as well. You can do this as follows:
    mkdir ~/public_html/codebase/edu/gac/max
    mkdir ~/public_html/codebase/edu/gac/max/movies
    cp -p ~max/public_html/codebase/edu/gac/max/movies/{Chain,Store,Movie,FindActor}.class \
      ~/public_html/codebase/edu/gac/max/movies
    
    (Again, the last two lines are conceptually one long line; the backslash tells the shell to treat them as thought they are one.)
  7. To run your server, you need to login to kilpinen, which you can do using the ssh command:
    ssh kilpinen
    
    Once on kilpinen, you can run your server. First, though, make sure that your public_html directory is readable by others, and that you have java in your command search path:
    chmod -R a+rX ~/public_html
    set path=(/usr/local/lib/java/bin $path)
    
    Now run your server as follows. This is two lines, one starting with setenv and one starting with java, even if it looks like more than two lines when displayed or printed (in the printout, it may be cut off):
    setenv CLASSPATH ~max/public_html/codebase:"$HOME/public_html/codebase:/usr/local/pgsql/java/lib/postgresql.jar"
    java -Djava.rmi.server.codebase='http://kilpinen.mcs.gac.edu/~'$USER'/codebase/' edu.gac.$USER.movies.dummy.ChainImpl
    
    Note that the second command won't return you to the prompt, until you kill it off as described later. You'll need to use a different terminal window for other commands. (There are other approaches, but this is the simplest to describe
  8. Now that your dummy server is running, try connecting to it from a client. First (and most simply) use the textual client, specifying jstudent-dummy-chain at the end of the command. To use your server with the applet client, you will need to access the web page on which the applet is, save it into a .html file in your www-docs directory, and edit your name in place of mine in the codebase and edit your chainServer name in. Then you can browse your .html file.
  9. To kill off your server, just type control-c in the terminal window where it is running. If you don't kill your server, it will stay running until kilpinen crashes or is rebooted. For a real server, we could arrange that it was automatically restarted when kilpinen is rebooted, but you probably won't want that for this lab. (I've tried to do it with my own servers; let me know if they ever aren't running.)

Now it is time to make a real server that connects using JDBC to your database (yielding three tiers), building on the work you did in the previous lab on SQL and JDBC. One issue you'll run into is that in this lab, we're looking for movies given an actor's full name (e.g., "Claude Rains") rather than seperate first and last names (e.g., "Claude" and "Rains"). To deal with this, you can use SQL's string concatenation operator, ||. For example, to just find Claude Rains in the persons table, you could do

select * from persons where fnames || ' ' || lname = 'Claude Rains';
Of course, you want to do a more complex query that also takes the other tables into account. Make copies of ChainImpl.java and StoreImpl.java in a different directory. Change the package lines to a different package as well. Change the name ChainImpl rebinds under from jstudent-dummy-chain to jstudent-chain. Now edit in the JDBC code, compile, deploy, and test as above. As with everything, ask if you need help.

If you have time on your hands and want to look at the client side, it is in the file

If you want, you could make a modified version, or replicate (or improve upon!) the textual client, the source of which I'm intentionally hiding.


Instructor: Max Hailperin