Getting Started

In this first example we'll show how simple it is to create a basic Web page using CL-HTTP. We are going to create a Web page at /demo.html on our local Web server. To avoid conflicts with the installed copy of Apache which is used by the Mac OS X system we will run CL-HTTP on port 8000, so the address for accessing our page on the local machine will be:


We first need to write a routine write-demo-page which will generate the HTML for the page. This response function takes two parameters; a URL object that stores parameters for the page, and the stream for generating the HTML. In its simplest form this will be:

(in-package :http-user)
(defun write-demo-page (url stream)
  "The response function for a simple computed URL."
  (declare (ignore url))
  (with-successful-response (stream :html)
    (format stream "<html>~%")
    (format stream "<head><title>Demo Page</title></head>~%")
    (format stream "<body><h1>Demo Page</h1><p>Welcome to our site!</p></body>~%")
    (format stream "</html>~%")))

This calls the CL-HTTP routine with-successful-response to return the HTML for the Web page to the specified stream. It then writes the HTML for the page using a series of format statements.

To make the page visible on the Web we need to export it to CL-HTTP, and specify the URL it should have, we use the CL-HTTP routine export-url:

(export-url "http://localhost:8000/demo.html"
            :response-function 'write-demo-page)

The routine export-url takes two parameters: the URL of the Web page, and the type of export; in this case :computed specifies that the response is computed by a Lisp function specified by the :response-function keyword.

Evaluating this makes the page available on the server, and we can connect to it from a Web browser on the local computer:


blog comments powered by Disqus