Appendix A. Example Software

Kit for Chapter 4

You can recreate the polling example using any URL-driven polling application. The Polls servlet discussed in Chapter 4 (and Chapter 10) is just one of many possible ways to do it. The point of the example in Chapter 4 is to show how and why it can be useful to reduce the user interface of a polling application to URLs that can be transmitted in email or newsgroup messages. That said, here are the pieces used in the example given in the book.

The Polls Servlet

To reproduce the example, you’ll need the Polls servlet (, plus a servlet host in which to run the Polls servlet. There are lots of servlet hosts available; see Polls and GroupCal, the two servlet examples in my book, are primarily set up to work with Jef Poskanzer’s Acme.Serve (, an open-source servlet host.

The documentation for the GroupCal servlet ( ) has details on compiling Acme.Serve for Unix or NT. Once you’ve done that, the setup for Polls is as follows:

  1. In

    Servlet Polls = new Acme.Serve.Polls(); 
      serve.addServlet( "/Polls", Polls );
  2. Recompile

  3. Run

    java Acme.Serve.Serve
  4. Test the Polls servlet:


The Poll-Creating Form

Here’s the HTML used to create the example form:

<form method="post" action="/cgi-bin/"> 
<p>Poll name: <input name="pname"> 
<br>1. <input name="1"> 
<br>2. <input name="2"> 
<br>3. <input name="3"> 
<br>4. <input name="4"> 
<br>5. <input name="5"> 
<p><input type="submit" value="Create Poll"> 

The Poll-Creating Form’s Handler

And here’s the script that handles the form. It pokes the poll-creating URL into the Polls servlet and responds with a set of pasteable URLs used to operate the poll.

To run the script, you’ll need two Perl modules: TinyCGI (included with the Docbase kit) and the CPAN LWP module.

#! perl -w 
use strict; 
use TinyCGI; 
use LWP::Simple; 
my $tc = TinyCGI->new; 
my $href = $tc->readParse(); 
print $tc->printHeader(); 
my $pname = $href->{pname}; 
my $host = "http://hostname:9090"; 
my $url = "$host/Polls?name=$pname"; 
foreach my $choice (keys %$href) 
  next if ( $choice eq 'pname' ); 
  next if ($href->{$choice} eq ''), 
  $url .= "&$choice=" . $tc->escape($href->{$choice}) ; 
get $url; 
print "<p>---- begin fragment to email or post to newsgroup ------------"; 
print "<p>Here are the URLs for voting in the poll:<br>"; 
foreach my $choice (sort { $a <=> $b } keys %$href) 
  next if ( $choice eq 'pname' ); 
  next if ($href->{$choice} eq ''), 
  print "<p>Poll: $pname, Choice: $href->{$choice}"; 
  print "<br><$host/Polls?name=$pname&vote=" .  
                $tc->escape($href->{$choice}) . ">"; 
print "<p>Here is the URL for viewing the tally:<br>"; 
print "<br><$host/Polls?name=$pname&tally=yes>"; 
print "<p>---- end fragment to email or post to newsgroup ------------";
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.