Before diving under the hood, here's a script that generates the traditional "Hello, world" greeting:
! HelloWorld.ls !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Demo script on http go to StartHere stop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! This is where the servlet GET and POST actions are handled. StartHere: clear document add "Hello, world!" write document stop
The operation of the script is as follows. First of all, anything on a line after a ! symbol is a comment and is ignored by the compiler. So there are only seven lines of code. The script starts with initialisation code. In this case it simply tells where to go when a GET or POST request is received. (Both are treated identically.)
StartHere: is a program label. When a GET or POST arrives the script fires up at that point. It clears the 'document', a notional representation of the HTML page that will be sent back to the client, then adds our famous message and writes the document back to the response output stream. That's it. The stop keywords put the servlet back into the idle state waiting for the next request.
To deploy this script, you compile it using the supplied compiler. Then you set up a server alias for the servlet supplied with the package, giving it the name of the script ("HelloWorld") as its single parameter. If all is well, anyone using the alias will be presented with the "Hello, world!" greeting.
The example above won't impress many visitors to your site. And never mind the content; this is a clumsy way of delivering what could have more easily been put into an HTML file. So let's do just that. Our second script will deliver any file named by a CGI parameter.
! Deliver.ls !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Deliver a file on http go to StartHere stop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! This is where the servlet GET and POST actions are handled. StartHere: clear document add template the directory cat "public/" cat parameter "File" write document stop
The only difference here is that the script delivers the contents of the file named by the "File" CGI parameter. The path to the file is made up of three components joined together with cat operators. The first component is the current directory, which establishes us in the local disk filing system. The second component is the path to the folder that contains the file to deliver. And the final component is the CGI parameter that names the file. The word cat is a string concatenation operator that joins them together. The word template tells the compiler to use the contents of a file rather than simply output a string.
So to deliver the file hello.html, use this URL:
http://myhost/alias?File=hello.html
where myhost is the name of your server and alias is the alias given to the servlet. This time, the parameter to the servlet alias is "Deliver" (the name of this script).
Although this is still less simple than allowing the server to deliver the file itself, it gets us closer to our target of writing dynamic content with the minimum of effort. So let's take the next step.
The simplest example of dynamic content is to ask the user for his/her name, then display that as part of a new HTML page. So we construct a page containing a form with a single text field called Name and a submit button. The user fills in the name field then clicks the button, and the following CGI request is sent to the server:
http://myhost/alias?Name=My+Name
As before, myhost and alias depend on the name of your server and the alias used for the servlet. The parameter Name is the name of the text field and My+Name is the name the user typed in, encoded as a URL string.
In the previous example we used a CGI parameter to name an HTML file to be delivered as it is. This time we will deliver a file but modify it on the way, to include the name we just received.
Here's a simple HTML file (only the <BODY> is shown):
<BODY> <P ALIGN="CENTER"><B><FONT SIZE="6">Hello, </FONT><FONT SIZE="6" COLOR="#CC0000">~NAME~</FONT><FONT SIZE="6"></FONT></B></P> <P ALIGN="CENTER"><B><FONT SIZE="6">Welcome to my world!</FONT></B> </BODY>
The only important part of this is the string ~NAME~, buried in the middle. We're going to substitute the name of the user as received, so we make sure the string is unique by adding some characters unlikely to be used in regular HTML. Save this file as hello.html.
Now we create a new servlet script:
! HelloUser.ls !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Deliver a personalized greeting. on http go to StartHere stop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! This is where the servlet GET and POST actions are handled. StartHere: clear document add template the directory cat "public/hello.html" where "~NAME~" is parameter "Name" write document stop
This one is again similar to the previous scripts, but this time we've added a where clause that substitutes every occurrence of ~NAME~ with the received Name parameter. The result is the user sees a personalized greeting screen.
That's enough for a general introduction. The rest of this document describes a real project; a set of records for a shop that rents videotapes. Everything will be done using a single servlet controlled by four scripts. All of the source code is supplied so you can get a real hands-on experience as you go. The best environment to work with is a pair of Windows 98 computers on a network; the minimum is a single Java-equipped workstation.