CS 5510 Homework 7

Due: Friday, November 6th, 2009 9:40am

Configuration: Save server.ss in the same directory as your program file. Then, you should be able to run

  (require "server.ss")
  (serve)

to start a web server on your machine at port 8080. 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 "server.ss")
  (serve 8001)

then access the server through a URL such as

  http://localhost:8001/g

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

Part 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 "j" j-handler)

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

  http://localhost:8001/j

Part 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"

Part 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.

Part 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: Thursday, December 3rd, 2009
mflatt@cs.utah.edu