From: Timothy J. H. <tim...@ma...> - 2004-07-28 16:54:33
|
On Jul 27, 2004, at 3:51 PM, Ken Anderson wrote: > So you start up jetty from inside JScheme. > Could we do that and run Tim's nice servlet stuff he just checked in? > Would we still need to package it up as a war? I've just checked in to CVS two little programs for running a Jetty server directly from Scheme. (They won't be available on public CVS for another 24 hours....) The first is about 40 lines (1.4KB) including comments and requires four jar files and requires you create a webapp (or a war file). > { > File: startserver.scm > Author: Tim Hickey, adapting code from David May's jetty example > Date: 7/28/2004 > > Use: copy jscheme.jar and startserver.scm to the toplevel of the > jetty folder > and then issue the following command: > % java -cp jscheme.jar jscheme.REPL startserver.scm > > This will start a server on port 8088. > } > > (use-module "elf/basic.scm" 'import 'all) > (use-module "elf/classpath.scm" 'import 'all) > (use-module "using/run.scm" 'import 'all) > > ; first we load in all of the jars from the lib and ext folders > ; we're actually only using 2 jars in lib/ and 2 in ext/ > ; javax.servlet.jar, org.morthbay.jetty.jar, jasper-runtime.jar > jasper-compiler.jar > > (for-each addClasspathUrl (files** (File. "lib") isJarFile)) > (for-each addClasspathUrl (files** (File. "ext") isJarFile)) > > > (define (start-server port context webapp) > (define server (org.mortbay.jetty.Server.)) > (define listener (org.mortbay.http.SocketListener.)) > (.setPort listener port); > (.addListener server listener) > (.addWebApplication server context webapp) > (.start server) > server > ) > > ; now startup two servers.... > (define server > (start-server 8088 "/jscheme" > "/Users/tim/Desktop/MyJetty/webapps/jscheme")) > (define server2 > (start-server 8090 "/" "/Users/tim/Research/Software/jscheme")) > The second is about 80 lines (2.8KB) and is included below. It requires only two additional jars: javax.servlet.jar org.mortbay.jetty.jar which run about 600KB total. This example allows you to create servlets directly in Scheme (with no war file or web.xml etc) and to associate them to contexts in your webserver.... > { > File: simpleserver.scm > Author: Tim Hickey (adapting some code of David May) > Date: 7/28/2004 > > Starting a servlet directly in Jetty without a webapp. > Here we show how to create two simple servlets running > in a webserver.... > > Use: > copy jscheme.jar and simpleserver.scm to the toplevel of the Jetty > folder, > and execute the following command: > % java -cp jscheme.jar jscheme.REPL simpleserver.scm > > This will start a server on port 8088 > } > > (use-module "elf/basic.scm" 'import 'all) > (use-module "elf/classpath.scm" 'import 'all) > (use-module "using/run.scm" 'import 'all) > > ; first we load in all of the jars from the lib and ext folders > ; This example only needs two jars from the lib folder: > javax.servlet.jar, org.mortbay.jetty.jar > (for-each addClasspathUrl (files** (File. "lib") isJarFile)) > > ; Next, create a server, a listener, and two ServletHttpContext (one > for each servlet we will create) > (define server (org.mortbay.http.HttpServer.)) > (define listener (org.mortbay.http.SocketListener.)) > (define time-servlet-context > (org.mortbay.jetty.servlet.ServletHttpContext.)) > (define dump-servlet-context > (org.mortbay.jetty.servlet.ServletHttpContext.)) > > ; Now we can put in some SchemeServlets > (define time-servlet-holder > (.addServlet time-servlet-context "time-servlet" "*.time" > "jschemeweb.SchemeServlet")) > (define dump-servlet-holder > (.addServlet dump-servlet-context "dump-servlet" "*.zz" > "jschemeweb.SchemeServlet")) > > > ; and define their functionality using Scheme code > ; (but don't initialize them until after the server is started!!) > > (define (init-time-servlet) > (define the-servlet (.getServlet time-servlet-holder)) > (define do_get > (lambda (request response) > (.println (.getWriter response) {The local time is > [(Date.)]\n\n}))) > (.do_get$ the-servlet do_get) > (.do_put$ the-servlet do_get) > ) > > (define (init-dump-servlet) > (define the-servlet (.getServlet dump-servlet-holder)) > (define do_get > (lambda (request response) > (.setContentType response "text/html") > (.println (.getWriter response) > {The servlet parameters are: <ol>[ > (map* (lambda(x) {<li>[x] -> [(.getParameter request > x)]</li>\n}) (.getParameterNames request))]</ol><br/>}))) > (.do_get$ the-servlet do_get) > (.do_put$ the-servlet do_get) > ) > > > > > (define start-it-up > (begin > (.setContextPath time-servlet-context "/js/*") > (.setContextPath dump-servlet-context "/js/*") > (.setPort listener 8088) > (.addListener server listener) > (.setContexts server > (list->array org.mortbay.jetty.servlet.ServletHttpContext.class > (list > time-servlet-context > dump-servlet-context > ))) > (.start server) > )) > > > ; Finally initialize the servlets > (init-time-servlet) > (init-dump-servlet) > > > > > I think it would be nice if a JScheme application could become a > webserver whenever it wanted to rather than have to always live inside > one. > > k > > > > ------------------------------------------------------- > This SF.Net email is sponsored by BEA Weblogic Workshop > FREE Java Enterprise J2EE developer tools! > Get your free copy of BEA WebLogic Workshop 8.1 today. > http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click > _______________________________________________ > Jscheme-user mailing list > Jsc...@li... > https://lists.sourceforge.net/lists/listinfo/jscheme-user |