Skip to content
Scala API for servlets in general and Jetty in particular
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


The aim of this pimped servlet API is to add the minimum to javax.servlet to
scalaize it. There is no routing engine, no ORM layer, just the usual scala 
pimping techniques. 

One good idea is to recast the servlet service methods as:

HttpServletRequest => HttpServletResponse => Unit

The HttpServletResponse => Unit part can then be factored into stock
objects called Responders. You can then concentrate on the 
HttpServletRequest => Responder part.

The result is not too shabby. It lets you write hello world like this: 

get("/") { 
    request => <h1>Hello {request("yourname") getOrElse "stranger"}!</h1>

Here the request is implicitly wrapped to allow easy parameter access
as in request("yourname") and the result, a scala.xml.Node, is implicitly 
converted to a suitable Responder.

OK, to be fair, lets expand that out with all the imports and proper XHTML:

package example
import Driver._       // start and stop Jetty
import Connectors._   // Jetty-specific connection methods such as listen()
import Handlers._     // Jetty-specific binding methods get("...") and post("...")
import Responders._   // generic responders and implicit conversions for responses
import Requests._     // generic request extractors and wrapper 

object HelloWorld {
  val host = ""
  val port = 9986
  // tell Jetty where to receive connections
  listen(host, port)

  // may be omitted since this is the default for XML responses
  implicit val contentType = ContentType("application/xhtml+xml")
  // hello world server
  get("/test") { request =>
    <html xmlns="">
      <head><title>The Test Page</title></head>
        <p>Hello {request("yourname") getOrElse "stranger"}!</p>

  def main(args: Array[String]) = { start; join }

Instead of querying the (wrapped) HttpServletRequest, you can use pattern matching.
Here is a KML service for google earth that shows this:

implicit val contentType = ContentType("application/")
// note 1

get("/cafe") {
  case Params(BBox(w, s, e, n)) =>   // note 2
    <kml xmlns="">
        for((name, lon, lat) <- findCafesIn(w, s, e, n)) yield
  case _ => 404 // note 3

The example extracts the coordinates of a bounding box (w, s, e, n) from a
Google Earth request and uses them to find a collection of locations via
findCafes(..), which is supposedly a spatial query on some data source.  

The locations are wrapped in KML markup and returned. 

This shows how to:

 * set an implicit content type (note 1); 
 * use extractors to obtain request parameters (note 2); 
 * implicitly convert responses such as XML or integers. 
   e.g an integer gets converted to an error response (note 3). 

There is a longer writeup here:
You can’t perform that action at this time.