In this section, we explain how to develop interactive web pages. These pages will typically prompt the user for some information (using a web form or a hyperlink) and then will generate a new web page, based on the user input. They may also perform other actions such as read/writing information on the servers disk, sending email, or accssing a database.
The language we use to specify these interactive web pages is a simple combination of Scheme, HTML, and CSS. For most of the examples in this section, the scheme will be used in relatively simple ways and hence you will not need to know much about the language itself beyond the few examples we demonstrate below. In Chapter ??, we will give a full introduction to Scheme and you can then use that to build even more complex servlets.
Dynamic Content and Scheme Servlets
The key idea behind servlets is that they provide a way to generate dynamic webpages, that is webpages in which the HTML is different each time someone visits it. Servlets can be specified in many different languages. In this book, we will look closely at servlets specified in Scheme.
To run these servlets you will need to have access to a server which has a Scheme webapp. The appendix explains how to download and install such a server. If someone can provide you access to a Scheme server then you will not need to download and install the server yourself.
Once you have access to a server, you create a scheme servlet simply by creating a file in the "webapp" folder and adding the suffix ".servlet" to the file name. When someone tries to view that file, the server will read the code that you have written in the ".servlet" file and will use that code to generate a new webpage which is then sent back to the user.
Let us now consider a few simple examples of Scheme servlets. These examples are not very useful by themselves but they allow us to explore the "idea" of Scheme servlets in a simpler context. The first example is the following program which date.servlet (run or (view) simply returns the date:
The first two lines are comments (as they begin with a semicolon) and so are ignored by Scheme. The second line is a Scheme expression which returns the current local date and time
Actually, (Date.) returns
a "Date" object representing the current time and date.
This Date object is turned into a string representing
the date in the local dialect.
To view this servlet, one must visit the URL:
MY.DOMAIN.EDU is the domain name of the
scheme server you are using. If you have installed a Scheme
server on your home computer, then you can use the IP
address of your home computer in place of MY.DOMAIN.EDU
or you can use the "self-loop" IP address
(Date.) is an example of a Scheme
expression. It is an invocation of the Date.
procedure with no arguments.
Arithmetic Expressions in Scheme
Another simple example of a
servlet is the following which we assume is stored in a
file named sumToTen.servlet:
+
to the arguments that follow it in the parenthesized list. The result that is return to the browser is the number
(* 3 3) and (* 4 4) to get 9 and 16
respectively. These values are subsituted back into the
expression to yield the simpler expression:
25 which is substituted
back into the expression to get:
Scheme provides a large set of arithmetic operators including the following
Interacting with HTTP headers
Another example of a servlet is the following which sends back a message to the browser (using the HTTP protocol) telling it to visit another page instead:
.sendRedirect and two operands:
response and "http://www.nsf.gov". The first
operand is a special one that refers to the page that is
sent back to the browser. The second operator is a string
of characters which is indicated by the enclosing double quotes.
There are two other "special" symbols used by the scheme
server: request and httpservlet. The request
symbol can be used to get information about the current
request. For example, the IP address of the browser that is
visiting the current page can be returned using the
following servlet:
Quasi-strings
The simplest type of useful servlet is one which just returns the same HTML each time to the user. These servlets are written as standard HTML files except that the file name must end in ".servlet" and the HTML ust be enclosed in a pair of curly braces as shown below:
This is a simple servlet
it has no dynamic content! }[(Date.)]
(This page is powered by Scheme servlets!) }
date.servlet servlet
tell the
server that the element they enclose is a Scheme
expression that should be run to get its value. That
value is then inserted directly into the page.
Returning to the date.servlet example,
if you then visit the URL
round operator then
rounds that number to the nearest whole number in the range
0 to 100.
As mentioned above, the curly braces in a servlet {} indicate that the enclosed text is to be sent verbatim to the client, except that text enclosed in square braces [] is first evaluated to get some "interesting" value which is then inserted into the text. This curly brace/square brace notation is called quasi-string notation. It is another example of a Scheme expression.
Strong Quasi-strings
Note that if you want to include any curly braces or square brackets in your webpage you have to put backslashes in front of them. The backslash indicates that the following character is to be viewed as just a character.[(Date.)] and because we're using the usual quasi-string notation we need to quote (or escape) curly braces and brackets like this \{ and this \} and \[ this and \] this }
#{....}#. You also use number signs to
flag the curly braces for escapes into dynamic code, like
#[...]# this.
If you use this syntax, then
you don't have to quote the curly braces inside the string.
#[(Date.)]# and because of the strong quasi-string notation we can write curly braces (like this { and this }) as well as square brackets (like this [ and this ]) without quoting them using back slashes. ]#
Quasi-strings and string-append
The general form of a quasi-string expression is as follows:
A, B, ..., C, and then
to insert the values one obtains in the corresponding places in the
string. There is a Scheme function string-append which can be used
to achieve a similar effect, e.g. instead of writing:
string-append expression.
