[plt-scheme] Module-based web servlet examples
Hi Norman,
> Can anyone point me towards examples of module-based servlets within the
> PLT Scheme web server?
> I feel rather foolish asking, because the interface as documented in the
> manual is so simple as to barely need illustration. But I can't get my
> noddy hello-world servlet to work, I can't find log files explaining
> _why_ it isn't working, and I'm starting to suspect I'm misunderstanding
> something fundamental.
>
> I would have thought that the following would work, based on the
> documentation, and the example in section 9.1 (plus the inclusion of the
> request-structs.ss module):
>
> (module echoing-servlet mzscheme
> (provide interface-version timeout start)
> (require (lib "request-structs.ss" "web-server"))
>
> (define interface-version 'v1)
> (define timeout +inf.0)
>
> ; start : request -> response
> (define (start rq)
> `(html
> (head (title "Echo servlet"))
> (body
> (h1 "Echo servlet")
> (table
> (tr (td "method")
> (td ,(request-method rq)))
> (tr (td "uri")
> (td ,(request-uri rq)))
> (tr (td "headers")
> (td ,(request-headers/raw rq)))))))
> )
I put this in
c:/Programmer/PLT/collects/web-server/default-web-root/servlets/echoing-servlet.scm
(well actually .../PLT-FULL-352.6/...
Ran
PLT Web Server Text.exe
[Huh? Why suddenly different names on Windows? And spaces
is a pain to enter in the shell.]
and loaded the address
<http://localhost/servlets/echoing-servlet.scm>
in my browser. It gave this page:
---
Erroneous Xexpr
An Xexpr in the servlet is malformed. The exact error is
Expected a string, symbol, number, comment, processing instruction, or
list, given #
The Full Xexpr Is
#
---
Looking at the source shows:
... given #<void></pre><h2>The Full Xexpr Is</h2>
<pre><font color="red"> #<void>
</font></pre>
So the problem is that the value #<void> is not allowed
to appear in an Xexpr.
Try this instead:
(module echoing-servlet mzscheme
(provide interface-version timeout start)
(require (lib "request-structs.ss" "web-server"))
(define interface-version 'v1)
(define timeout +inf.0)
(define (->string o)
(cond
[(string? o) o]
[(symbol? o) (symbol->string o)]
[(void? 0) "#void"]
[else "[extend ->string]"]))
; start : request -> response
(define (start rq)
`(html
(head (title "Echo servlet"))
(body
(h1 "Echo servlet")
(table
(tr (td "method")
(td ,(->string (request-method rq))))
(tr (td "uri")
(td ,(->string (request-uri rq))))
(tr (td "headers")
(td ,(->string (request-headers/raw rq))))))))
)
On my computer it gives:
Echo servlet
method get
uri [extend ->string]
headers [extend ->string]
Why? Well, the uri is an url-structure from (lib "net.ss")
and headers is an environment consisting of a list of
symbol-string pairs.
See
<http://download.plt-scheme.org/doc/352/html/web-server/web-server-Z-H-12.html#node_idx_126>
Btw - if you want to test your servlet directly in DrScheme, here is a
trick:
Add
(require (planet "web.scm" ("soegaard" "web.plt" 1 0)))
to your program, choose the module language, click run, and then try
Welcome to DrScheme, version 352.6-svn3oct2006.
Language: (module ...).
> (start (make-test-request))
(html
(head (title "Echo servlet"))
(body (h1 "Echo servlet")
(table (tr (td "method") (td "get"))
(tr (td "uri") (td "[extend ->string]"))
(tr (td "headers") (td "[extend ->string]")))))
See <http://planet.plt-scheme.org/300/docs/soegaard/web.plt/1/0/doc.txt>
for documentation on make-test-request.
Hmm. I wanted to give an example with bindings, but it seems the
definition of both the request structure and what constitutes bindings
has changed, since I wrote the code.
Jay: Is the documentation on bindings still up-to-date?
<http://download.plt-scheme.org/doc/352/html/web-server/web-server-Z-H-12.html#node_idx_126>
-
Jens Axel Søgaard