Writing web-server-side Kawa scripts

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

In either case, the result of evaluating the top-level expressions becomes the HTTP response that the servlet sends back to the browser. The result is typically an HTML/XML element code object; Kawa will automatically format the result as appropriate for the type. The initial result values may be special "response header values", as created by the response-header function. Kawa will use the response header values to set various required and optional fields of the HTTP response. Note that response-header does not actually do anything until it is "printed" to the standard output. Note also that if a "Content-Type" response value is printed that controls the formatting of the following non-response-header values.

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.