Bootstrapping A Web Server In Scala
Solution 1:
I know Max alread mentioned it, but I couldn't resist pointing out Scalatra's 6 lines hello world:
import org.scalatra._classScalatraExampleextendsScalatraServlet {
get("/") {
<h1>Hello, world!</h1>
}
}
Anyway, take a look at available Scala web frameworks.
EDIT
There's some discussion about how easy is to get the tooling ready, particularly with regards to Lift. So, here's a session on Ubuntu. Most of my time was spent trying to figure out where did Sun's Java go in the package manager. Anyway, once Java was installed, this is how it went, with all messages elided, so one can see what I actually had to type:
dcs@dcs-desktop:~$ wget -q -O bin/sbt-launch.jar http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.4.jar
dcs@dcs-desktop:~$ echo'java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"' > bin/sbt
dcs@dcs-desktop:~$ chmod u+x bin/sbt
dcs@dcs-desktop:~$ mkdir app
dcs@dcs-desktop:~$ cd app
dcs@dcs-desktop:~/app$ sbt
Project does not exist, create new project? (y/N/s) s
> *lifty is org.lifty lifty 1.4
> lifty create project-blank sample 2.1
> reload
> update
> jetty-run
There, web server running. Of course, you have to know about SBT and Lifty beforehand, to even know you'd use them to get a Scala Lift program running, but, on the other hand, I had never heard about Flask, so I'd certainly spend way more time trying to figure out how to get a web server application going in Python than I did getting a Lift one.
I also did not get it right on the first try -- I tried going for Scala 2.8.1 (the above uses a default 2.7.7 version, though 2.8.0 will work too), only to find out that there's no Lift version available for that version of Scala as yet. On the other hand, I had lifty installed already, and de-installed it just to show the command that installs it.
I do wish there was a Debian/Ubuntu package for SBT -- it's just a tiny shell script and a jar file, after all, and it takes care of downloading Scala, Lift, etc, and at whatever version you need.
It is a different model than Python and Ruby, where the language comes with a package manager which handles most things.
Solution 2:
You might find Unfiltered worth a look.
Solution 3:
Well, there's Scalatra, which aims to be analogous to Ruby's Sinatra in terms of functionality and ease of use.
Solution 4:
This solution uses a JAX-WS Endpoint:
import java.io._
import javax.xml.ws._
import javax.xml.ws.http._
import javax.xml.transform._
import javax.xml.transform.stream._
@WebServiceProvider@ServiceMode(value=Service.Mode.PAYLOAD)classPextendsProvider[Source] {
def invoke(source: Source) = newStreamSource( newStringReader("<p>Hello There!</p>"));
}
valaddress="http://127.0.0.1:8080/"
Endpoint.create(HTTPBinding.HTTP_BINDING, newP()).publish(address)
println("Service running at "+address)
println("Type [CTRL]+[C] to quit!")
Thread.sleep(Long.MaxValue)
You can copy it to a file WebServer.scala and run it simply by typing:
scala WebServer.scala
Solution 5:
You could use an embedded Jetty Server:
/*
* Required Libs: Jetty, Servlet API
*
* Compile:
* scalac -cp jetty-6.1.14.jar:jetty-util-6.1.14.jar:servlet-api-2.5-6.1.14.jar WebServer.scala
*
* Run:
* scala -cp .:jetty-6.1.14.jar:jetty-util-6.1.14.jar:servlet-api-2.5-6.1.14.jar WebServer
*/import org.mortbay.jetty.Server
import org.mortbay.jetty.servlet.Context
import javax.servlet.http.{HttpServlet,
HttpServletRequest,
HttpServletResponse}
classHelloServletextendsHttpServlet {
override def doGet(req : HttpServletRequest, resp : HttpServletResponse) =
resp.getWriter().print("Hello There!")
}
object WebServer {
def main(args: Array[String]) {
valserver=newServer(8080)
valroot=newContext(server, "/", Context.SESSIONS)
root.addServlet(classOf[HelloServlet], "/*")
server.start()
println("Point your browser to http://localhost:8080/")
println("Type [CTRL]+[C] to quit!")
Thread.sleep(Long.MaxValue)
}
}
In case you target for a LOC comparison, You use the HTTP server embedded with the Sun JDK. Another solution could be to use javax.xml.ws.Endpoint and the Provider API.
Post a Comment for "Bootstrapping A Web Server In Scala"