AltServlet

AltServlet is a Java servlet that uses Mozilla's Rhino library to create a platform for developing JavaScript-based web applications.

It implements a solid module system that parallels Java's own package system, making it easy to write clean modular JavaScript code.

The primary purpose is a basis for the the Alt Framework, but it is not limited to this.

Rationale

My original intention with the Alt Framework was to build on top of Helma Object Publisher, a similar JavaScript web framework based on Java and Rhino, but I soon found it too limiting.

As a challenge, I decided to see how difficult it would be to write a basic Java Servlet that could serve JavaScript files. After two days of coding, I had functional prototype.

After fine tuning the module system for JavaScript, I decided to stick with it.

Read about the module system

HttpServlet

The purpose of the Alt Framework is not to reinvent the wheel (which on the surface seems hypocritical). As such, AltServlet is based on the robust Java Servlet technology.

Aside from the module system for loading JavaScript files and resources, AltServlet provides a transparent interface to the Java Servlet functions.

With every page request is evaluated in a scope containing the request and response Java objects, of types HttpServletRequest and HttpServletResponse, respectively.

Thanks to Rhino's automatic Java wrapping, these Java objects can be used as if they were native JavaScript objects.

Scripting in AltServlet

Rhino provides an interesting mix of Java and JavaScript. While you can write pure JavaScript with AltServlet, the true power lies in using its ability to script Java.

Rhino makes all Java classes and functions transparent in JavaScript, and in almost all cases, you can simply write Java code verbatim in JavaScript. To access classes you can either use the Packages class or the global java package.

Examples:

var d = new java.awt.Dimension(100,200);

Or you can make aliases:

var Dimension = java.awt.Dimension;
var d = new Dimension(100,200);

You can methods and access member variables just as you would in Java:

var area = d.width * d.height;
d.setSize(800,600);

In addition to regular syntax, Rhino adds a few convenience features for JavaBean-style access. For example, if a class defines methods such as getXxx and setXxx, you can access them like member variables.

var url = new java.net.URL("http://marcello.cellosoft.com/");
var port = url.port; // same as url.getPort()

Useful links:

AltServlet Dynamic Scope

AltServlet implements dynamic scope. This means allows modules to be defined in their own scope and for separate requests to each have their own scope.

This can be illustrated as follows:
RhinoServlet Scopes

You can read any variable from a parent scope as if it were a local variable, but any variable or function you define will be defined in the current scope. This will shadow any variable or function in a higher scope, but not replace them.

You can access the global scope directly with the global variable.

You can access modules by writing module.submodule. Submodule scopes are not children of their parent modules, so a script in foo.bar cannot access definitions in the foo module without prefixing them with foo..

More about Rhino's dynamic scope feature

page last updated: Thursday, February 14, 2008

copyright © 2006 cellosoft