true Blue
Scripting with servlets [jruby] - part I (Sun Java System WebServer 7.0)
Writing Jruby servlets on Sun Java System WebServer 7.0.
Prior work:
Jruby already has an implementation of servlets by Marcin Mielżyński
It has these features:
- Loosely based on PyServlet (servlet implementation for jython)
- Mimics RailsControllers
- Offers three kinds of servlets
StatefullServlet that behaves like Rails controller (if instantiated every request)
StatelessServlet that behaves like standard stateless Java servlet
SessionServlet - is kept transparently in session (is its instance
variables are not shared between sessions)
How ever it also has these features that I did not like
- Too Rails oriented.
- Servlets in ruby should be preferably similar to java servlets rather than look like Rails
- Does too many things in java.
- We should do only the basic framework in java and delegate the rest to the ruby world. This way a programmer who
is more comfortable in ruby will be able to implement any feature that he wants with out being impeded by
the implementation of our servlet code in java. More over, the code in java generally becomes immutable once it
gets compiled and added into a jar.
It would be much more nicer to delegate the responsibility of doing these things to the handler in ruby rather than to do it in java.
This would allow the possibility of allowing the programmer to construct different varieties of handler servlets in ruby itself during
the development with out touching the java code (compiling and jar-ing)
Our approach.
Making use of the ScriptServlet developed in the previous entry, we create a simple class that provides necessary initialization
and evaluation primitives.
package com.sun.servlet; |
Here we use the initialize method to read in the handler specified, and executes it. The handler (which is in ruby) is responsible
for specifying the behavior of our servlet. It specifies which HTTP methods are supported, and how the Request URI is interpreted.
The eval method provides a way for the scripts that are in place for HTTP method processing to be executed by the ScriptServlet.
The Ruby Dimension
Jvm Bindings for jruby.
Jruby allows us to access the methods of a class or instance the same way as that of java, by using the 'dot' notation.
Thus to print something to the stdout, we can use the statement
System.out.println 'my string'
with the jruby correctly invoking System.out.println in java.
Thus our handler can be written as below.
docroot/WEB-INF/code/ruby.rb
proc {|httpservlet| |
We return a proc object with an arity 1 to the evaluator. This is done to allow the servlet initializer to pass the
httpservlet object as an arguement. We could also have bound the httpservlet to a global variable.
Inside the larger anonymous proc, we define a second proc for do_get, and bind it to the 'get' and 'post' methods of httpservlet.
these procs take request and response as the arguments. They extract the name of the script and load and evaluate the script
referenced. The script that gets evaluated has complete access to the variables request, response, out and httpservlet just like
a normal java servlet or a jsp page.Any exceptions are printed to the response output stream.
A simple ruby script that may be loaded by this handler will look like this.
/docroot/hello.rb
def myhello |
Build Steps.
A complete webapp is provided here. You can download it and extract the contents to a
directory called 'jruby' inside your installation. In order for the build.xml to work, It should be in
the samples/java/webapps/jruby directory in your installation of webserver.
It also contains the jruby.jar in the WEB-INF/lib, which should be replaced with the latest
jruby.jar if necessary.
Your extracted directory will look like this.
|cd jruby
|find .
./docs
./docs/index.html
./src
./src/build.xml
./src/RubyServlet.java
./src/docroot
./src/docroot/WEB-INF
./src/docroot/WEB-INF/lib
./src/docroot/WEB-INF/lib/jruby.jar
./src/docroot/WEB-INF/web.xml
./src/docroot/WEB-INF/sun-web.xml
./src/docroot/WEB-INF/code
./src/docroot/WEB-INF/code/ruby.rb
./src/docroot/index.html
./src/docroot/hello.rb
./src/ScriptServlet.java
./deploy.tcl
You can run 'ant' from inside the src directory which will create the jruby-webapp.war in the
jruby directory. This war file can be deployed on the webserver using the wadm.
wadm -u admin -f deploy.tcl
Once the deployment goes through, you will be able to access the ruby file using the url
http://yourserver:port/jruby/hello.rb
Posted at 06:31PM Jan 14, 2007 by Rahul Gopinathan Nair in scripting | Comments[2]
Posted by A. Sundararajan on January 16, 2007 at 12:43 PM IST #
Posted by rahul on January 16, 2007 at 02:58 PM IST #