The Polls Servlet

As we saw in Chapter 4, the Polls servlet presents a simple web API used to create a new instance of a poll, cast votes, and view the tally. Table 10.1 illustrates the Polls API.

Table 10-1. The Polls Servlet’s Web API

Action

URL

Create a new poll

/Polls?name=Picnic99&1=Sat+June+12&2=Sat+June+19&3=Sun+June+20

Cast a vote

/Polls?name=Picnic99&vote=Sun+June+20

View the tally

/Polls?name=Picnic99&tally=

At the heart of Polls is a Java hash-of-hashes (HoH)—that is, a Java Hashtable whose keys are the names of poll instances (such as Picnic99) and whose values are hashtables. The keys of each interior hashtable are the names of the choices in that poll (e.g., “Sat June 19”), and the values count the votes for each of these choices. Table 10.2 depicts these structures.

Table 10-2. The Polls Servlet’s Central Data Structure

Key: poll name

Value: Hashtable

Picnic99

key: choice name

value: vote count

 

Sat June 12

12

 

Sat June 19

5

 

Sun June 20

14

Preferred HMO

Tufts

3

 

Matthew Thornton

11

 

Harvard Pilgrim

7

In Perl, as in Java, it’s easy to create this kind of HoH. But a Perl CGI script can’t so easily meet the following requirements:

  • Retain the object in memory across multiple invocations of the script.

  • Protect the object from concurrent use by multiple clients.

  • Retrieve the object from disk at start-up and keep the in-memory version synched with the on-disk version as updates occur.

Larry Wall likes to say that Perl aims to makes easy ...

Get Practical Internet Groupware now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.