s c h e m a t i c s : c o o k b o o k

/ Cookbook.WebDispatchOnLink

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Dispatch On a Link Being Clicked

Problem

You are writing a servlet in the PLT web server and want to choose an action based on which link was clicked. You cannot use a form submit button.

Solution

Use send/suspend/dispatch

;; send/suspend/dispatch : Response -> void
(define (send/suspend/dispatch p-exp)
 (let/cc k0
  (send/back
   (replace-procedures
     p-exp (lambda (proc)
             (let/cc k1 (k0 (proc (send/suspend k1)))))))))

(define (replace-procedures p-exp p->a)
  (cond
    ((list? p-exp) (map (lambda (p-e) (replace-procedures p-e p->a))
                        p-exp))
    ((procedure? p-exp) (p->a p-exp))
    (else p-exp)))

A simple example of its use:

(define (callback req)
   `(html (head (title "Page Two"))
          (body (h1 "Page Two"))))

(send/suspend/dispatch
   `(html (head (title "Page One"))
          (body (h1 "Page One")
                (p (a ((href ,callback)) "Page Two")))))

Discussion

A clumsy way to handle this situation is to append a parameter to the URL:

`(a ((href ,(string-append url "?arg=suchandsuch"))) "click here")

This is not recommended: the web server will add this parameter to all the next 'continuation' URL it gives you and you will have to manually strip it out.


Comments about this recipe

Based on a posting to plt-scheme by Mike Burns. Only handles the xexpr datatype, which makes it mostly useless for those using more advanced (SXML, WebIt!) frameworks?

-- NoelWelsh - 31 Aug 2004

This is a variant on my original send/suspend/dispatch. It's clean, but doesn't give the programmer access to the generated URL. For a version that does that, please see my paper at:

http://continue.cs.brown.edu/research/

Having the URL is useful for appending anchor references to the URL, or for using the URL in JavaScript?.

-- PeteHopkins - 23 Nov 2004

I suggest renaming this solution as send/suspend/callback to avoid confusion with the original by PeteHopkins?.

-- DanielSilva - 11 Jan 2005

It would be nice if send/suspend were implemented on top of a web-server internal version of PeteHopkins?'s send/suspend/dispatch, like this:

(define (send/suspend build-page)
  (let/cc cc
    (send/suspend/dispatch
       (lambda (embed/url)
          (send/suspend (embed/url cc))))))

-- DanielSilva - 11 Jan 2005

Contributors

-- NoelWelsh - 31 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: WebRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest