Javascript Server 0.2 ===================== This is a simple example of how to use the Rhino Javascript implementation as the language for implementing Java Servlets. From within Javascript I use Rhino to start up an instance of the Jetty Web Server. Once started it's possible to create servlets from Javascript itself and have these accessed via a web browser. Although this example is very simple it leads to some interesting ideas: 1) Using Javascript as both the server and client scripting language. Validation rules could be defined in Javascript and executed on the client, server or both as required. 2) Rhino has continuations support. A continuation based web server could be implemented relatively easy using this approach. I've included the JAR files for Rhino, XMLBeans and Jetty needed for this example to work. The version of Jetty used was Jetty 6.0 beta 16 obtained from [1]. The Rhino version was Rhino1_6R2 obtained from [2]. The XMLBeans version was 1.0.4 and was obtained from [3]. [1] http://jetty.mortbay.org/jetty6/ [2] http://www.mozilla.org/rhino/ [3] http://xmlbeans.apache.org/ Build ===== You will need to build the 'JavascriptServlet.java' file for this example to work. This class derives from HttpServlet and provides an abstract method that can be called from Javascript. I only overload the GET operation here. It's simple to extend to PUT and I'll do that in a later release. To compile this class: javac -classpath servlet-api-2.5.jar JavascriptServlet.java Running ======= You can run the example.js from Rhino by starting Rhino like this: java -classpath .;servlet-api-2.5.jar;jetty.jar;jetty-util.jar;js.jar;xbean.jar org.mozilla.javascript.tools.shell.Main This will work from Windows. For Linux or similar Unix like operating system you will need to replace the semicolons (;) with colons (:): java -classpath .:servlet-api-2.5.jar:jetty.jar:jetty-util.jar:js.jar:xbean.jar org.mozilla.javascript.tools.shell.Main Once done, load the 'example.js' file (Don't type the 'js>', it's the prompt: js> load("example.js"); You can start a server on a port with 'startServer': js> var s = startServer(8080); :INFO: Logging to STDERR via org.mortbay.log.StdErrLog :INFO: jetty 6.0.0beta16 :INFO: Started SelectChannelConnector @ 0.0.0.0:8080 A servlet in Javascript is really an object containing the methods that will be used to replace the abstract methods in JavascriptServlet. The simple HelloWorld method in example.js is: { ProcessGet: function (req, resp) { var text = "Hello World!"; resp.setContentType("text/plain") resp.setContentLength(text.length) resp.getOutputStream().print(text) resp.flushBuffer() } } If you know Java servlets this should look fairly familiar. For the simple example here you need to wrap this with a call to 'makeServlet' which creates an object with some things Jetty needs (class loaders, names, etc): /* A simple HelloWorldServlet. It provides a definition for the abstract ProcessGet method in JavascriptServlet */ HelloWorldServlet = makeServlet({ ProcessGet: function (req, resp) { var text = "Hello World!"; resp.setContentType("text/plain") resp.setContentLength(text.length) resp.getOutputStream().print(text) resp.flushBuffer() } }); Once the servlet is created it can be added to a running server: js> addServlet(s, "/", HelloWorldServlet); You can now access this serverlet with http://localhost:8080/ You can add the GoodbyeWorldServet from the example.js: js> addServlet(s, "/bye", GoodbyeWorldServlet); Note the different path and servlet. This can be reached with http://localhost:8080/bye/ The HTMLServlet example shows how to use E4X, the extension to Javascript for using XML natively, to generate HTML: HTMLServlet = makeServlet({ ProcessGet: function (req, resp) { var doctype = ''; var result =
Hello from E4X on: {new Date()}
The date above was generated dynamically by calling new Date().