CS 3520 Homework 7   - Due October 21

This homework is due October 21, 8:00 AM.

Configuration: Install plai-server.plt.

After installing plai-server.plt, you should be able to run

  (require (lib "plai-server.ss" "plai-server"))
  (serve)

to start a web server on your machine at port 8080. This server is the same as in server.ss, so it has the a, b, g, h, and i servelets. Access the server in your web browser (on the same machine) with a URL such as

  http://localhost:8080/a

The serve function accepts an optional argument for the port number, so if you run

  (require (lib "plai-server.ss" "plai-server"))
  (serve 8001)

then access the server through a URL such as

  http://localhost:8001/g

In addition to importing the serve function, (require (lib "plai-server.ss" "plai-server")) imports add-handler, web-read/k, and web-pause/k.

Exercise 7.1, Big Hello

Follow these two steps to add a servlet j to your web server:

  1. Define j-handler:

      (define (j-handler base args)
        `(font ((size "+2")) "Hello"))
    The expression `(font ((size "+2")) "Hello")) corresponds to the HTML <font size="+2">Hello</font>.

    In general, HTML of the form <tag attrib="value" ...>content</tag> can be represented as `(tag ((attrib "value") ...) content), and literal content (such as the word Hello) should be quoted as a string.

  2. Register j-handler as the servlet j:
      (add-handler #rx"/j" j-handler)
    The #rx form generates a regular-expression pattern to match the path part of the incoming URL.

Run the server and access your new servlet with a URL like this:

  http://localhost:8001/j

Exercise 7.2, Mad Libs 1

Create a servlet m that asks the user for a noun, an adjective, and an adverb (one by one), and then return a setence that the server makes up by substituing the three words into a template.

For example, the server's template might be "my ___ saw a ___ rock falling ___". In that case, if the user supplies cat, yellow, and lazily, the server's response will be "my cat saw a yellow rock falling lazily". Make up your own template.

The function format can help in assembling a response. The first argument to format is a string containing some number of ~as. Each additional argument supplies text to replace a corresponding ~a. For example:

  (format "my ~a saw a ~a rock falling ~a" 'cat 'yellow 'lazily)
  ; => "my cat saw a yellow rock falling lazily"

Exercise 7.3, Mad Libs 2

Create a servlet n that is just like m, but with a different template. Obviously, m and n should share the implementation for getting three words from the user.

Exercise 7.4, Mad Libs 3

Create a servlet p that is just like n, but it should use two adjectives, a verb, and a noun. Generalize the word-getting code used by m and n so that it works with a supplied list of word types. You should end up with one implementation of the word-getting part of the servlets.

(It's a good idea to keep your ungeneralized code, though, to show to the grader — and possibly to the rest of the class — how you arrived at the generalized code.)


Last update: Wednesday, November 23rd, 2005
mflatt@cs.utah.edu