[plt-scheme] Parameters and servlets

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Feb 15 19:17:46 EST 2006

I think there is either a bug in my understanding of
module servlets or the web-server, but I can't figure
out which.

Consider the small servlet below, which consists of

  parameter-test-required
    contains a parameter, which is initialized
    when the module is required

  parameter-test
    the servlet, which shows when the other
    module was required

In theory it works. In practice I get a series of pages with
this contents:

   Servlet Parameter Test
   1140048030

At some point the servlet is removed from the cache,
and then I expected to see a new time -- but in stead
I get this:

   The following error occured:
   number->string: expects argument of type <number>; given #f

I.e. the parameter suddenly has the original value and not
the one given by evaluting the expression in the module top-level.

In load-servlet/path in

<http://ja.soegaard.net/planet/html/collects/web-server/dispatch-servlets.ss>

dynamic-require is used to get hold of start. After reading
"5.5. Dynamic Module Access" and "5.6 Re-declaring Modules" I
had doubts whether dynamic-require worked the same way as I
expected, so I tried this little test in the DrScheme repl:

   > (define start (dynamic-require "parameter-test.scm" 'start))

   > (start 'foo)
   (html (head (title "Servlet Parameter Test")) (body (h1 "Servlet
   Parameter Test") "1140048605"))

   > (start 'foo)
   (html (head (title "Servlet Parameter Test")) (body (h1 "Servlet
   Parameter Test") "1140048605"))

   > (define start (dynamic-require "parameter-test.scm" 'start))

   > (start 'foo)
   (html (head (title "Servlet Parameter Test")) (body (h1 "Servlet
   Parameter Test") "1140048605"))

   > (start 'foo)
   (html (head (title "Servlet Parameter Test")) (body (h1 "Servlet
   Parameter Test") "1140048605"))

Which works just as it is supposed to do.



The servlet code:

(module parameter-test mzscheme
   (require (lib "servlet.ss" "web-server")
            "parameter-test-required.scm")

   (provide interface-version timeout start)
   (define interface-version 'v1)
   (define timeout +inf.0)

   ; start : request -> response
   (define (start initial-request)
     (report-errors-to-browser send/finish)
     `(html (head (title "Servlet Parameter Test"))
            (body (h1 "Servlet Parameter Test")
                  ,(number->string (get-time)))))
   )


(module parameter-test-required mzscheme
   (provide get-time)

   (define load-time (make-parameter #f))

   (define (get-time)
     (load-time))

   (load-time (current-seconds)))



-- 
Jens Axel Søgaard





Posted on the users mailing list.