You don't know how to get started with web development using the PLT Web server.
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 (planet "instaservlet.ss" ("untyped" "instaservlet.plt" 1)))
(define (start initial-request)
'(html
(head
(title "Demonstration Page"))
(body
(h1 "Demonstration Page")
(p "This is a simple servlet demonstration page"))))
(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:
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)))
(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")
(lib "external.ss" "browser"))
(send-url "http://127.0.0.1:5678/?v=1;url=2;title=aaa")
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!
--
NoelWelsh - 27 Nov 2006
--
StephenDeGabrielle - 15 Jan 2008