Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


2006-06-02

Server side Javascript

An update to the code demonstrating E4X support is here.

Ajaxian is reporting that Sun is releasing Phobos, a Javascript application server. This is a web server that runs Javascript for implementing the server side code. The source has not yet been released, according to Ajaxian, but will be.

A while back I worked on doing server side Javascripting using Rhino as the interpreter and Jetty 6 as the server. I got it to work but never tidied it up for release. Sun's Phobos has motivated me to make it available so I've tidied it up and it can be downloaded from javascript-server.tar.gz. A darcs repository with the code is here:

darcs get http://www.bluishcoder.co.nz/repos/javascript-server

The repository contains the Jetty 6 JAR files and the Rhino interpreter JAR, along with an example Javascript file showing how it works and a readme.

Once the 'example.js' is loaded into the Rhino interpreter you can start a Jetty 6 web server on port 8080 with the following command:

var s = startServer(8080);

A simple 'HelloWorld' style servlet looks like this in Javascript:

HelloWorldServlet = makeServlet({ 
  ProcessGet: function (req, resp) {
    var text = "Hello World!";
    resp.setContentType("text/plain")
    resp.setContentLength(text.length)
    resp.getOutputStream().print(text)
    resp.flushBuffer()
  }
});

It has a 'ProcessGet' function which is called when an HTTP GET is made. The 'req' and 'resp' objects and the standard Java HttpServletRequest and HttpServletResponse respectively.

The magic of deriving from HttpServlet is done by Rhino. But it can't actually override HttpServlet methods - it can only override abstract methods. To fix this I created a JavascriptServlet Java class which forwards doGet(...) to an abstract 'ProcessGet', which is the method you see overridden above.

With the Jetty server we started running from the interpreter earlier we can add this servlet dynamically:

addServlet(s, "/", HelloWorldServlet);

Now requests to http://localhost:8080/ will run HelloWorldServlet. When can dynamically add other servlets too:

addServlet(s, "/bye", GoodbyeWorldServlet);

Requests to http://localhost:8080/bye/ will run the GoodbyeWorldServlet.

Using the Jetty API you can work out how to add, remove and otherwise do some very cool stuff. Like play with Jetty 6's Ajax continuation support.

The server can be stopped with:

s.stop();

Running Javascript on the client and server gives some interesting possibilities. Sharing code for example. Or writing validation rules in Javascript which run both on the client and server.

The Dojo javascript toolkit can run client side or server side. This means you could use Dojo's packaging and other nice features for server side development in Javascript.

Rhino has continuation's which are serialisable. A continuation based web server could be implemented which serialises continuations like SISCWeb.

Download javascript-server, play with it, and let me know what you do with it. I hope to follow up on some of the above ideas too.

Tags


This site is accessable over tor as hidden service mh7mkfvezts5j6yu.onion, or Freenet using key:
USK@1ORdIvjL2H1bZblJcP8hu2LjjKtVB-rVzp8mLty~5N4,8hL85otZBbq0geDsSKkBK4sKESL2SrNVecFZz9NxGVQ,AQACAAE/bluishcoder/-30/


Tags

Archives
Links