Due: Thursday, October 20th, 2011 10:45am
Configuration: Save server.rkt in the same directory as your program file. Then, you should be able to run
(require "server.rkt") (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.rkt") (serve 8001)
then access the server through a URL such as
http://localhost:8001/g
In addition to importing the serve function, (require "server.rkt") imports add-handler, web-read/k, and web-pause/k.
Follow these two steps to add a servlet j to your web server:
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.
The base argument to a handler part of the requested URL, and you can ignore it. The args argument reflects the query part of the URL; it is a list of pairs, where each pair has a symbol and string. For example, the query part guitar=doug;bass=slim;fiddle=woody;accordion=joey is represented as (list (cons 'guitar "doug") (cons 'bass "slim") (cons 'fiddle "woody") (cons 'accordion "joey")). The Racket assoc function is handy for finding a pair in an args list based on its symbol, and use cdr to extract the string from a pair.
(add-handler "j" j-handler)
Run the server and access your new servlet with a URL like this:
http://localhost:8001/j
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"
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.
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 may need the Racket apply function to apply format to a list of arguments.
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, October 19th, 2011mflatt@cs.utah.edu |