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 Instaservlet program takes care of configuring the Web server and your servlet for most common configurations. The other WebRecipes and the Web server documentation should help answer any questions with the actual programming of servlets.

To create a servlet with Instaservlet, you need only write a function from a request to a response. You don't have to write any configuration code. So, for example, here is a simple servlet that displays a demonstration page:

#lang scheme/base

;; Require Instaservlet
(require (planet "instaservlet.ss" ("untyped" "instaservlet.plt" 1)))

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

;; Run the servlet
(go! start)

Run this code and you can view the resulting webpage at http://localhost:8765/

By default Instaservlet runs the web server on port 8765, but this, and many other configuration options, can be changed by passing keyword arguments to go!. For example, to run on port 1234:

(go! start #:port 1234)

Here's a slightly more involved example of 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:

#lang scheme/base

(require
   web-server/servlet
   (planet "instaservlet.ss" ("untyped" "instaservlet.plt" 1)))
  
;; start : request -> response
(define (start initial-request)
  (define binding-list (format "~s"(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 "text:"  binding-list))
                       )))))

(go! start #:port 5678)

You can then launch a browser to visit this servlet, using:

(require 
 (lib "url.ss" "net")
 ;; required to launch the browser with
 (lib "external.ss" "browser"))

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

Instaservlet currently works only under PLT Scheme 4. It you use 372 or an older version, you can still use Instaweb, which provides a front end to setup the Web server to run a single servlet, but does not provide a default servlet configuration. Here's the first example using Instaweb. First write 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 all!


Comments about this recipe

Contributors

-- NoelWelsh - 27 Nov 2006 -- StephenDeGabrielle - 15 Jan 2008

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