hello world from scalatra

There’s been some buzz lately about Scalatra, which is a Sinatra like framework for Scala.  Sinatra is a very scaled down platform for web programming in Ruby.  Unlike many web frameworks (such as Lift, JSF, etc.) which tend to be very stateful and try to abstract away from the HTML and web technologies, Sinatra does no such thing.  It’s very low level, which can make it very fast to get up and going for smaller, stateless web applications, especially REST based services.  Scalatra is an attempt to bring that to the Scala world.

To create our Hello World webapp in Scalatra, we’ll first create a dummy project.  Create a new folder to hold our project and use the simple build tool (sbt) to create your project.

md HelloScalatra
cd HelloScalatra
sbt

You’ll get output like this:

Project does not exist, create new project? (y/N/s) y
Name: HelloScalatra
Organization: test
Version [1.0]:
Scala version [2.7.7]: 2.8.0
sbt version [0.7.4]:

Now you need to tell the simple build tool what additional libraries you need for your project. We’ll create a project build file to do that. This works kind of like a Gemfile in the sense that it lets the build tool know what dependencies we have. The build file is written in Scala itself.

Create the file ScalatraBuild.scala in the HelloScalatra/project/build folder.

// save as project/build/HelloScalatraBuild.scala
import sbt._

class ScalatraBuild(info: ProjectInfo) extends DefaultWebProject(info) 
{
  // scalatra
  val sonatypeNexusSnapshots = "Sonatype Nexus Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
  val sonatypeNexusReleases = "Sonatype Nexus Releases" at "https://oss.sonatype.org/content/repositories/releases"
  val scalatra = "org.scalatra" %% "scalatra" % "2.0.0-SNAPSHOT"
 
  // jetty
  val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.22" % "test"
  val servletApi = "org.mortbay.jetty" % "servlet-api" % "2.5-20081211" % "provided"
}

Now download the dependencies. This is similar to running bundle install in a Rails app.

sbt update

One drawback of Scalatra is that it is a Servlet-based framework. That means we aren’t getting away without creating a web.xml file. So let’s set that up. Since simple build tool uses maven conventions, we need to do that in the HelloWorld/src/main/webapp/WEB-INF folder.

HelloWorld/src/main/webapp/WEB-INF/web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>HelloWorld
        <servlet-class>com.test.HelloWorld
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld
        <url-pattern>/*
    </servlet-mapping>
</web-app>

This says that we will have a Servlet handled by the com.test.HelloWorld.scala file (which we haven’t written yet) and it will respond on all URL patterns.

Now we can actually write our app. Simply create the HelloWorld/src/main/scala/com/test/HelloWorld.scala file:

HelloWorld/src/main/scala/com/test/HelloWorld.scala:

package com.test

import org.scalatra._

class HelloWorld extends ScalatraServlet {
    get("/") {
        "Hello World"
    }
}

Now we can start the web server:

sbt jetty

And we can browse to http://localhost:8080/ and we’ll see:

Hello World