To begin, it’s important to note that there are roughly two ways to leverage Rhino on the server: via CGI, and via the Java Servlet API.
I don’t have too much to say about this. The main thing you need to know in order to run Rhino as CGI is how to set it up to run with a shebang.
Here’s an example of a minimal Rhino CGI script:
#!/usr/bin/env rhino print("Hello world!");
After that, it’s mostly a matter of setting up your web server to run .js files as CGI.
First, here’s a tarball of the project in case you’re interested in exploring my implementation: RhinoServlet.tar.gz
For a .js file to inherit the servlet API by extending the javax.servlet.http.HttpServlet class, then, it must be compiled with a “-extends javax.servlet.http.HttpServlet” command-line argument, and javax.servlet.http.HttpServlet must be on the classpath. Ant does all of the heavy lifting, then, both setting up the classpath, and using jsc to compile with all appropriate command-line arguments.
Here’s the relevant ant task that does this work:
TinyServlet.js, then is able to implement the servlet API functions in the global namespace. In this way, doPost, doGet, and the other familiar servlet API methods, will actually override those of the HttpServlet class. TinyServlet.js, then, becomes, in effect, a real subclass of HttpServlet, with no Java-language host context required.