Back Previous Next


2 A real project

2.1 Outline

The application I will describe will manage a simple videotape lending library. To do this we need to keep records of our stock, our customers and which of the former are currently out on loan to the latter. This is a classic relational database application; the job of the servlet is to manage the database, to provide data entry screens and to display information from the database on demand.

This application is assumed to be based on an intranet, not on the World Wide Web. It will be accessed by the store owner, either from the store or from his apartment above the store. Hence the advantage of hosting it on a Web server and not simply doing it using Access. Of course, there are many applications that require WWW access to do a similar job, this is to illustrate the advantage of taking a network view of a problem even though there may be no immediate need to have access to data from more than one location.

2.2 The development environment

To build an intranet application based on a web server you first need the server. To avoid unnecessary complication I suggest you do the work on a PC that doesn't already host Web or intranet services. For this purpose I recommend the use of vqServer, an all-Java Web server that can be downloaded and used freely. It requires no installation; just unpack and run. Administration is done via a browser, either on the same machine or any other on the same network.

Next you need a database. Again, downloadable software comes to the rescue in the form of InstantDB, an all-Java SQL database. Please read the license terms carefully; this software is only free for non-commercial use. Make sure the idb.jar file is present in your CLASSPATH.

And you must of course have a Java 1.1 runtime environment (or a full JDK), which you can download from Sun. Also be sure to download the servlet extension classes.

vqServer comes with its own executable for Windows, but if you are using another OS or prefer not to add the InstantDB classes to your normal CLASSPATH you can put all support classes into the 'classes' directory of vqServer and launch it from the following command line:

jre.exe -cp classes/classes.zip;classes/idb.jar;classes/idb.jar vq.server.console

Once you have set up your server, copy all the files from our 'public' and 'servlet' directories to the same locations in your server hierarchy. (This alone provides a good reason for not using a live server.) The documentation you are reading will now be available simply by starting a browser and requesting the default page from the server.

2.3 File organization

On a Web server, HTML files available for anyone to access are kept in the 'public' folder. Because most of the files used in this application are 'templates' (I'll explain that below) they are unsuitable for general access. So we'll keep them in a templates folder inside the 'servlets' folder, where they can't be accessed by accident.

All access to this system will be via our scripted servlet. To get into the system we need to call the servlet, which is done with the following URL:

http://hostname/library

In this URL, hostname is the address of the server, e.g. localhost or 192.168.12.4 etc. The word library is a servlet alias set in the server to point to the servlet. Every server has its own way of setting up servlet aliases, but the important information is that the servlet is called lserve (that's a lower-case 'L' at the start) and it has a single parameter whose name is "script" and whose value is "Library". The servlet is a Java class file (supplied as part of this package), and Library is the name of the primary script we will be building.

Since no parameters are specified in the above URL, the script needs to take the default action of calling the top-level menu for our library management system. This is in the templates folder and it's called mainmenu.html. If you examine this file you'll find it contains six active links, each of which looks something like this:

library?ScreenID=CAdd

where the value of the ScreenID parameter is different in each case. The word library is the alias for our servlet, as described above; there's no need to give the full URL as we're staying on the same server.

Here's the script (with comments removed):

	on http go to StartHere
	stop

StartHere:
	clear document
	add template the directory cat "servlets/templates/mainmenu.html"
	write document
	stop

The menu screen has six clickable lines, each of which sends a different value of parameter ScreenID back to the servlet. But before we look at these I suggest you test what we have so far, as follows:

2.4 Compiling and testing

In order to use a script it must first be compiled. To do this, first create a text file called Library.ls (the extension stands for Linguist Script), put the listing above into it and save it into the servlets folder of your web server. If you don't like typing you'll find in the scripts folder a number of files called Library-???.ls; these are the various steps along the way to the final version. This one is Library-1.ls, which can be copied to the servlets folder and renamed to Library.ls.

Now open a console window, navigate to the servlets directory and issue the command

java LS Library.ls -o
or
jre -cp . LS Library.ls -o

This will create a file called Library.lrun (or will complain about errors, in which case they'll need to be fixed). This is the compiled script file, ready to run under the control of the lserve servlet. Now run up your server and make sure it has an alias called library whose real name is servlets/lserve and whose single parameter "script" has the value "Library".

Open your browser, either on the same computer or on another on the network, and call the library servlet as described above. Check the URLs of each of the six active links to confirm the correct URL has been substituted in each case.

There are a hundred and one things that can go wrong in the above procedure; I must leave you to sort them out. But it's essential to get a simple script working before things get too complex.


Back Previous Next