Performance Zone is brought to you in partnership with:

Jos works as Architect for JPoint. In the last couple of years Jos has worked on large projects in the public and private sector. Ranging from very technology focusses integration projects to SOA/BPM projects using WS-* and REST based architectures. Jos has given many presentations on conferences such as Javaone, NL-JUG, Devoxx etc., and has written two books for Manning: Open Source ESBs in Action and (published in the next couple of months) SOA Governance in Action. In this last book Jos shows how, with some good practical governance approaches, you can create great WS-* and REST based services and APIs. Besides this he has his own blog where he writes about interesting technologies and shares his ideas about REST, API Design, Scala, Play and more. Jos is a DZone MVB and is not an employee of DZone and has posted 54 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started with Scala and Scalatra

  • submit to reddit

Scalatra is a lightweight scala based micro-web framework, that can be used to create high performance websites and APIs. In this tutorial we'll get started with the installation of scalatra and import our test project into Eclipse.

SBT and giter8

Before you can get started you need to install a couple of tools (I'm assuming you've got a JDK 1.6+ installed). I'll give you the condensed installation instructions, and extensive version of this can be found at the following scalatra page ( This approach should work for most environments, for my own however, it didn't work... It downloaded old versions of giter8 and an old version of sbt. For sbt I used macports to get the latest version:

port install sbt

And I also downloaded giter8 manually.

curl | sh
cs n8han/giter8

This last command will install g8 in your /bin directory. I've tested this with this g8 version:

giter8 0.5.0
Usage: g8 [TEMPLATE] [OPTION]...
Apply specified template.
    -b, --branch
        Resolves a template within a given branch
        Set given parameter value and bypass interaction.
Apply template and interactively fulfill parameters.
    g8 n8han/giter8
    g8 git://
Apply template from a remote branch
    g8 n8han/giter8 -b some-branch
Apply template from a local repo
    g8 file://path/to/the/repo
Apply given name parameter and use defaults for all others.
    g8 n8han/giter8 --name=template-test

Create initial project

Go to the root directory where you keep you projects and run the following:

jos@Joss-MacBook-Pro.local:~/dev/scalatra/firststeps$ g8 scalatra/scalatra-sbt
organization [com.example]: org.smartjava
package []: org.smartjava.scalatra     
name [scalatra-sbt-prototype]: hello-scalatra
servlet_name [MyScalatraServlet]: HelloScalatraServlet
scala_version [2.9.1]: 
version [0.1.0-SNAPSHOT]:

This will create a hello-scalatra folder in the directory your in that contains the project.


To test if everything is working, go into this directory and use sbt to start the application from sbt with 'container:start'. This will download a lot of stuff, and finally start the application:

jos@Joss-MacBook-Pro.local:~/dev/scalatra/firststeps/hello-scalatra$ sbt
[info] Loading project definition from /Users/jos/Dev/scalatra/firststeps/hello-scalatra/project
[info] Set current project to hello-scalatra (in build file:/Users/jos/Dev/scalatra/firststeps/hello-scalatra/)
> container:start
[info] jetty-8.1.5.v20120716
[info] NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
[info] started o.e.j.w.WebAppContext{/,[file:/Users/jos/Dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
[info] started o.e.j.w.WebAppContext{/,[file:/Users/jos/Dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
15:12:44.604 [pool-6-thread-4] INFO  o.scalatra.servlet.ScalatraListener - Initializing life cycle class: Scalatra
[info] started o.e.j.w.WebAppContext{/,[file:/Users/jos/Dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
15:12:44.727 [pool-6-thread-4] INFO  o.f.s.servlet.ServletTemplateEngine - Scalate template engine using working directory: /var/folders/mc/vvzshptn22lg5zpp7fdccdzr0000gn/T/scalate-5609005579304140836-workdir
[info] Started SelectChannelConnector@
[success] Total time: 1 s, completed Sep 7, 2012 3:12:44 PM
[success] Total time: 1 s, completed Sep 7, 2012 3:10:05 PM

To test if everything is correct point the browser to localhost:8080 and you'll see the following screen:


Import in Eclipse

I usually develop with Eclipse, so in this case, lets make sure we can edit the source code in Eclipse. For this we can use sbt eclipse plugin. Adding this plugin is very easy. Go to the 'project' folder and add the following (with an empty line before it) to the plugins.sbt file.

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")

When you next run sbt, you'll see a lot of stuff being downloaded. From sbt run 'eclipse' and eclipse configuration files (.classpath and .project) will be created. Now start up the Eclipse IDE and you can import the project you just created (make sure you also install the scala-ide for Eclipse).

Scala - hello-scalatra_src_main_scala_org_smartjava_scalatra_HelloScalatraServlet.scala - Eclipse - _Users_jos_Dev_workspaces_workspace-scala.jpg

Now we can start the container, and let sbt listen for changes in our resources.

$ sbt
> container:start
> ~ ;copy-resources;aux-compile

Any time we save a file in Eclipse, sbt will copy and recompile the resources. So we can directly see our changed files in the browser. Open the file HelloScalatraServlet and change the welcome text. If you save this, sbt will reload the application and you'll directly see the changed files.


That's it for this tutorial!

Published at DZone with permission of Jos Dirksen, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)