Friday, January 9, 2009

Configuring VRD for Scripting Languages

In traditional java web applications, you can only use java language to write server side code. Using VRD-SLE (Scripting Language Extension), you can easily configure your java web applications to write server side code using Jython, JRuby, Groovy and JavaScript. Not only this, you can also get benefit of the powerful libraries of Jython and JRuby.

The first thing you need to do is to configure your web application to work with Vroom Request Dispatcher. For that please read the Blog Entry What is Vroom Request Dispatcher?

Once the above step is complete, you need to add following entries in /WEB-INF/vrd-config.xml file: (You may only include the entries that are needed)

<vrd-config>
<request pattern="/path/to/scripts/.*.py">
<invocation method-name="net.openkoncept.vroom.vrd.jython.JythonRequestDispatcher.dispatch"/>
</request>
<request pattern="/path/to/scripts/.*.rb">
<invocation method-name="net.openkoncept.vroom.vrd.jruby.JRubyRequestDispatcher.dispatch"/>
</request>
<request pattern="/path/to/scripts/.*.groovy">
<invocation method-name="net.openkoncept.vroom.vrd.groovy.GroovyRequestDispatcher.dispatch"/>
</request>
<request pattern="/path/to/scripts/.*.js">
<invocation method-name="net.openkoncept.vroom.vrd.javascript.JavaScriptRequestDispatcher.dispatch"/>
</request>
</vrd-config>

For Jython, you need to add JVM parameter as follows:

-Dpython.path=/path/to/jython-home/Lib:/path/to/jython-home/Lib/site-packages
Like servlet, there is an entry point for these scripts and that is a no argument public method named vrd_main , to request and response objects are accessible in the script using variables vrd_hreq (javax.servlet.http.HttpServletRequest) and vrd_hres (javax.servlet.http.HttpServletResponse). Below are the samples in each scripting language:

Jython:

def vrd_main():
res = vrd_hres
res.contentType = 'text/html'
w = res.writer
w.print('Hello World')
w.flush()


JRuby:

def vrd_main()
res = $bsf.lookupBean('vrd_hres')
res.contentType = 'text/html'
w = res.writer
w.print('Hello World')
w.flush()
end


Groovy:

def vrd_main() {
res = vrd_hres
res.contentType = 'text/html'
w = res.writer
w.print('Hello World')
w.flush()
}


JavaScript:

function vrd_main() {
res = vrd_hres
res.contentType = 'text/html'
w = res.writer
w.print('Hello World')
w.flush()
}

No comments:

Post a Comment