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")))) )
run-demo.ss which contains:
(require (planet "instaweb.ss" ("schematics" "instaweb.plt" 1))) (instaweb "demo.ss" 8765)
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 serverNow visit the URL and check that the servlet is working. That's it!
(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)) ))))))
(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
(send-url "http://127.0.0.1:5678/?v=1;url=2;title=aaa") ;; launch browser
| CookbookForm | |
|---|---|
| TopicType: | Recipe |
| ParentTopic: | WebRecipes |
| TopicOrder: | 999 |