Wednesday, April 25, 2007

An Introduction to Web Development with PLT Scheme - An Intermezzo


This is the third post in a series programming web applications with PLT Scheme. In part one and two the model and view of a small reddit-like application were discussed. Before turning to the control part of this application, we need to look at the basic operation of the PLT Scheme web server.

Requests and responses - the big picture

What happens when a user clicks on a link to your web-site? The browser sends a request to the web-server. The request holds among other information the protocol version and the uri (uniform resource identifier) of the requested resource. The web-server must now compute a response and send it back.

How the response is computed depends on the requested resource. In the case of static content, the response of the web-server consists of a few headers (including the mime-type of the file) and a verbatim copy of the file.


Dynamic content is handled by servlets. A servlet is a small program that given a representation of a request computes a response, which the web-server then sends back. The standard "Hello World"-servlet reads as follows:
(module hello-world mzscheme
(provide interface-version timeout start)
(define interface-version 'v1)
(define timeout +inf.0)

; start : request -> response
(define (start initial-request)
`(html (head (title "Hello World"))
(body ([bgcolor "white"])
(p "Coffee is bliss.")))))

A module based servlet must provide a start function, that given a request computes a response. In this case the response is XHTML represented as an X-expression. If your fancy is classical HTML as strings, you can return a list, whose first element is the mime type as a byte string, and whose second element is a list of strings (or byte strings) with the lines of the document. For more advanced purposes (such as generating incremental responses) one can return a response structure - see the documentation for details.

A little tip: If you place a call to report-errors-to-browser at the beginning of start, then the web server will catch any otherwise uncaught exceptions, generate an error page (with source location information) and send it to the client. This is considerably more convenient than constantly rumaging through log files.


Back to the request structure. A request structure holds:
  • the method (get, post, ...)
  • the uri of the resource
  • the headers (including cookies)
  • bindings (from name value pairs either from a posted form or from the query url itself)
  • the host-ip (in case the same web server instance handles more than one ip)
  • the client ip
  • raw data from a post operations
The net collection contains many valuable utilities to deal with uri, headers, cookies etc. I have collected these and other utilities in a little web framework available from PLaneT. It also contains some syntax that hopefully, makes it a bit easier to write servlets. The philosophy is to let the servlet parse the request and store the information in parameters. Similarly parameters hold what will become assembled to a response, when the servlet is finished. For simple servlets, like the one above, nothing much is gained, but more advanced usage patterns like post-redirect-get become much easier to code. The last part of this series will use the framework to implement the control.

Try the PLT Web Server

The PLT Web Server is included in the DrScheme download. If you start "PLT Web Server.exe" (Windows) or "web-server-text" (unix and mac), then the static content will be served from "c:/Programs/PLT/collects/web-server/default-web-root/htdocs" and the servlets are in will be in "...default-web-root/servlets/". A few examples are included.


Blogger Chris said...

Another heart felt thanks form a scheme newcomer.

I encourage you to continue the series as you are providing an invaluable service (IMHO).

I think that 5 to 10 years ago every language was trying to show how "complete" it was by having a GUI framework. Now I think that a web framework is a more accurate evaluation of a language.

I've been playing with all the new toys (Ruby on Rails, Lua via Kepler, Python with and Pylons) trying to find something that rivals the productivity of with c# (my day job).

I have a side project that I have great latitude in choosing a language and I would love to use plt scheme.

Can you elaborate on any experience you have with building a public web site? Or as an alternative just keep posting these great tutorials?


Blogger Jens Axel Søgaard said...

Thanks for the kind words.

Can you elaborate on any experience you have with building a public web site?

Is there anything in particular you have in mind?

Or as an alternative just keep posting these great tutorials?

The code for the last part is done, so it "just" a question of getting time to write down a blog post.

Blogger Shriram Krishnamurthi said...

Hi pragmatic,

Yes, every language now wants to have a Web programming framework...but PLT Scheme has been at it since 1997, and quite successfully. And even today there are many ways in which the PLT framework is more advanced than what those other languages offer.

I've built multiple non-trivial software packages (that people have even paid to use) using PLT Scheme. There are some issues, of course, but as we encounter them we fix them. Jay McCarthy, for instance, is making regular progress towards making the server infrastructure much more scalable and usable, and many others (such as Jens, Noel Welsh, etc.) are contributing to the overall effort.

Blogger Ido Yehieli said...

The code for the last part is done, so it "just" a question of getting time to write down a blog post.

Even just posting the complete code would be imho better than nothing... We could play with it & use it in the mean while until you get to writing the post.

Blogger Jens Axel Søgaard said...

Hi Ido,

Mail me, and I'll send you a copy.

Blogger Stephen said...


FYI a related video...reddit in lisp.



PS thanks for the good articles - keep em coming!


Post a Comment

<< Home