Building a Dynamic Website

To try out our shiny new web server, let’s build a simple site that greets a visitor, using the dirt-simple function hello-request-handler:

(defun hello-request-handler (path header params)
   (if (equal path "greeting")
       (let ((name (assoc 'name params)))
          (if (not name)
              (princ "<html><form>What is your name?<input name='name' />
 </form></html>")
             (format t "<html>Nice to meet you, ˜a!</html>" (cdr name))))
       (princ "Sorry... I don't know that page.")))

This hello-request-handler function supports only a single web page, called greeting. The first step in serving up this greeting page is to see if this page is indeed what the client requested . If not, we print an apology to the user for not finding the specified page . Otherwise, we check the request parameters to see if we know the user’s name. If not, we ask the user to enter a username using a web form . If we do know the user’s name, we greet the visitor enthusiastically.

Note

We’re taking a ton of shortcuts with our web server and this primitive website. For instance, any HTML sent to a client should be wrapped in a proper HTML skeleton, such as <html><body>...</body></html>. However, even then our page wouldn’t be fully compliant with modern HTML standards. In addition, when a client requests a nonexistent page, the appropriate response is to display a 404 error page, not just print a polite apology. Luckily, web browsers are very forgiving about such shortcuts, and they will display our simplified responses anyway.

Testing the Request Handler

Before we launch our new website, let’s test our hello-request-handler in the REPL by first viewing a page about lolcats:

> (hello-request-handler "lolcats" '() '())
Sorry... I don't know that page.

Perfect. As you can see, when we ask our request handler for a page other than the greeting page, it just prints an apology. Now let’s try viewing the correct greeting page:

> (hello-request-handler "greeting" '() '())
<html><form>What is your name?<input name='name' /></form></html>

Excellent! Our request handler has generated an HTML form asking the user for a username. Now let’s pass in a parameter for the user’s name, as if the form had been processed and sent to the server:

> (hello-request-handler "greeting" '() '((name . "Bob")))
<html>Nice to meet you, Bob!</html>

Because of the way we designed our web server, it’s very simple to debug a request handler independently in the REPL. We were able to see that hello-request-handler generates the correct responses without actually firing up a web browser.

Launching the Website

Now that we know that our new website is functioning, let’s launch it! But first, we need to make sure that all of the functions discussed in this chapter have been defined in an instance of CLISP. If you haven’t been entering these functions into the REPL as you’ve been reading, you can just save them all into a file called webserver.lisp, and then load them with (load "webserve'").

Once you’ve defined your functions in the CLISP, start the server by entering the following into the REPL:

> (serve #'hello-request-handler)

That’s it! Now you should be able to visit the site in a web browser:

image with no caption
image with no caption

As you can see, when you visit our greeting page from a browser (using 127.0.0.1:8080, which will point to port 8080 on the same machine the web browser is running on), you are asked for your name. The server then shows a follow-up page, which greets you by name. This shows that our web server was able to parse out the name from the request parameters, and was able to pass the name to our hello-request-handler function.

We now have a fully functioning web server and request handling infrastructure. In future chapters, we’ll use these tools to create an awesome, graphical, web-based game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.130