Scala, ZXTM and Java Extensions

Scala LogoThere has been a lot of talk over the past few years about new and different languages targeting the JVM as their runtime platform. Scala has been one of the most high-profile languages in the JVM vanguard. Most notably, in the past year Twitter has announced that it is migrating away from Ruby on Rails to Scala because of RoR's lack of scalability.

Scala is a neat hybrid language which is both object oriented and functional. Its best aspect is that you can utilize all of the standard Java libraries without writing any Java code. Check out http://www.scala-lang.org/ for more information on the language itself, as I'm just going to talk about how to get a simple 'Hello World' example working with ZXTM.

Why is it possible for us to use Scala in place of Java with ZXTM? Well, since Scala targets the JVM we can just import the compiled class files into ZXTM and they are recognized in the same way as any Java class file. The only extra step we need to take is to import the Scala libraries into ZXTM, so that they are available to the same Java runtime. This additional file, scala-library.jar, can be found in your Scala distribution and all you have to do is upload it to ZXTM using the Java Extensions Catalog.

If you want more information on Java Extensions in ZXTM, then have a look at the related articles Introducing Java Extensions and "Hello, World!" Java Extension.

Ok, let's look at a simple hello world example in Scala.

import javax.servlet.http._;
import com.zeus.ZXTMServlet._;
class MyServlet extends HttpServlet
{
override def doGet(zreq: HttpServletRequest, zres: HttpServletResponse)
{
zres.setContentType("text/html");
var out = zres.getWriter();
out.println("Hello World");
}
}

Quite simple and no magic, we just extend an HttpServlet as we would using Java and act on the Response object providing some content.

Put this in a file MyServlet.scala and compiling is just a matter of invoking the Scala compiler.

> scalac MyServlet.scala

This will produce a class file, MyServlet.class, which you will need to upload to ZXTM. ZXTM looks at class files and determines which can be called via TrafficScript, creating stub TrafficScript rules as necessary. Attach this new rule to the HTTP VirtualServer of your choosing as a request rule and see your Scala code in action.

Now you are all set to work with Scala in ZXTM!

UPDATE: Incorporated changes suggested by James Iry.

Dec [Zeus Dev Team] 30 June 2009 Bookmark with del.icio.us Post this article to Digg Post this article to reddit Post this article to Facebook Tweet this article 2 comments  

Comments:

This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.

Comment from: James Iry [Visitor]
Since the servlet container is responsible for managing the lifecycle of servlets, the use of "object" doesn't really make sense here. In Scala an "object" declared at top level is a singleton. The use of an object is also why you got two class files. One holds the definition of the class to which the singleton belongs and the other holds the (lazily instantiated) static reference.

If you rewrote your code as "class MyServlet extends HttpServlet..." you'd only get one class file, MyServlet.class, and the solution would be that much more indistinguishable from Java.

PS: for some reason this blog is rejecting a perfectly valid url for my site "http://james-iry.blogspot.com/"
Permalink 02 July 2009 @ 14:55
Comment from: Owen Garrett [Zeus Dev Team]
James - the blog subscribes to a central blacklist of words and URLs, and for some reason, *.blogspot.com was in the list.

I've whitelisted it locally... for everyone else's benefit, James' site is http://james-iry.blogspot.com
Permalink 02 July 2009 @ 15:50
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

Recently...

Other Resources