Node:Server-side scripts, Next:, Up:XML tools



Writing web-server-side Kawa scripts

You can take a Kawa program, in any supported language, and run it as either servlet engine (using a web server that supports servlets), or as a "CGI script" on most web servers.

Here is a simple program hello.scm:

(require 'http) ; Required for Scheme, though not BRL/KRL.
(response-content-type 'text/html) ; Optional
(make-element 'p
  "The request URL was: " (request-url))
(make-element 'p
  (let ((query (request-query-string)))
    (if query
      (values-append "The query string was: " query)
      "There was no query string.")))
#\newline ; emit a new-line at the end

The same program using KRL is shorter:

<p>The request URL was: [(request-url)]</p>,
<p>[(let ((query (request-query-string)))
    (if query
      (begin ]The query string was: [query)
      ]There was no query string.[))]</p>

You can also use XQuery:

<p>The request URL was: {request-url()}</p>
<p>{let $query := request-query-string() return
    if ($query)
    then ("The query string was: ",$query)
    else "There was no query string."}</p>

Either way, you compile your program to a servlet:

kawa --servlet -C hello.scm
or:
kawa --servlet --krl -C hello.krl
or:
kawa --servlet --xquery -C hello.xql

The next two sections will explain how you can install this script as either a servlet or a CGI script.