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

/ Cookbook.WebGettingStartedInstaweb

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

Getting Started With Web Development and Instaweb

Problem

You don't know how to get started with web development using the PLT Web server.

Solution

The instaweb program takes care of setting up the Web server for most common configurations. The other WebRecipes and [the Web server documentation should help answer any questions with the actual programming of servlets.

Instaweb provides a front end to setup the Web server to run a single servlet. Let's say you have a servlet in a file demo.ss:

(module demo mzscheme

  (require (lib "servlet.ss" "web-server"))
  
  (provide interface-version
           timeout
           start)

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

  (define (start initial-request)
    '(html
      (head
       (title "Demonstration Page"))
      (body
       (h1 "Demonstration Page")
       (p "This is a simple servlet demonstration page"))))
  
  )

To setup the Web server to use this servlet, create another file called, say run-demo.ss which contains:

(require (planet "instaweb.ss" ("schematics" "instaweb.plt" 1)))

(instaweb "demo.ss" 8765)

Now load run-demo.ss into DrScheme (or MzScheme) and Execute it. You should see output like:

Web server started on port 8765
Visit URL http://localhost:8765/servlets/demo.ss
Type stop to stop the server and exit
Type restart to restart the server

Now visit the URL and check that the servlet is working. That's it!

Discussion

This is great!

Here is an example of using Instaweb to with a servlet that gets arguments sent to the servlet, along with some embedded javascript (intended to be bookmarked) that allows you to extract select text and metadata (url and title) from an arbitrary page and send it to the servlet.

(module snatcher mzscheme
  (require 
   (lib "servlet.ss" "web-server"))
  (provide interface-version timeout start)
  (define interface-version 'v1)
  (define timeout (* 60 60 24))
  
  ; start : request -> response
  (define (start initial-request)
    (define binding-list (format "~s"(request-bindings initial-request)))
    
;    (define url (extract-binding/single
;                 'url
;                 (request-bindings initial-request)))
;    (define title (extract-binding/single
;                   'title
;                   (request-bindings initial-request)))
;    (define text (extract-binding/single
;                  'text
;                  (request-bindings initial-request)))
      
      
    `(html (head (title "A Test Page"))
           (body ([bgcolor "white"])
                 (p "Query;"  
                    (div ((class="aboutsmall"))
                         (p "Drag this link: " (a ((href "javascript:location.href='http://127.0.0.1:5678/?v=1;url='+encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)+';text=\\r'+escape(document.getSelection().replace(/\\s/g,' ').replace(/ {2,}/g,' '))") (title "post to snatcher") (onclick "window.alert('Drag this link to your bookmarks toolbar, or right-click it and choose Bookmark This Link...');return false;") ( class "bookmarklet2")) "post to snatcher") " up to your Bookmarks Toolbar.")
;                         (p ,(string-append "URL:" url))
;                         (p ,(string-append "Title:" title))
;                         (p ,(string-append "text:" text))
                         (p ,(string-append "text:"  binding-list))
                         ))))))

Instaweb would be called

  (require 
   (lib "url.ss" "net")
   (lib "external.ss" "browser")  ;; required to launch the browser with
   (planet "instaweb.ss" ("schematics" "instaweb.plt" 2 2)))
  
  (instaweb   ;; launch
            #:servlet-path "snatcher.ss" ;;filename of servlet
            #:port 5678) ;; port


you can then launch the browser with;

(send-url "http://127.0.0.1:5678/?v=1;url=2;title=aaa") ;; launch browser

-- StephenDeGabrielle - 14 Jan 2008


Comments about this recipe

Contributors

-- NoelWelsh - 27 Nov 2006

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