5.2. COUNTERS AND FILES
67
There are various techniques for getting around this, but the general lesson
is that passwords are not helpful unless they are very carefully hidden! We will
discuss security in more detail later.
There are other ways of restricting access beside asking for a password.
Restricting access to certain IP addresses is a common way of creating an
intranet like interface for your servlet. For example, Brandeis University
owns all IP addresses have the form
129.64.*.*
where the two asterisks represent any integers between 0 and 255. One can test
whether the user's IP address is from Brandeis using the following test:
(.startsWith (.getRemoteAddr request) "129.64.")
Similarly, one can restrict access to computers running the MacIntosh Operating
System or to all IP address except a few forbidden ones.
5.2
Counters and Files
Next we show how to create servlets that read and write to files on the server.
Some of the simple applications this enables are servlets that contain counters,
and servlets that allow you to leave a message.
We first consider the problem of implementing a servlet that keeps track of
how often it has been visited. There are many strategies one can employ for
such counters. For this example, we will write a servlet counter.servlet that
stores its count in a file called counter.servlet count.
The code for the counter.servlet servlet is in Figure 5.2. This servlet
introduces a few new ideas. First of all, it relies on a small library files.scm
of scheme procedures for reading and writing to the count file. We assume that
this file is loaded when the server is first started up.
The servlet must first load the library before it can use the special counter
library procedures. This is done using the load procedures
(load "webapps/scheme/lib/forms.scm")
Also, since the servlet now does two things:
update a counter and
generate a webpage,
we must use a feature of the let* expression that we haven't mentioned so far
the ability to do several things before computing the value to be returned by
the expression. The general form of the let* is
(let* ((V1 E1)
(V2 E2)
...