Fast Web Development With Scala

I am currently experimenting with Scala. It seems quite convenient for web applications. Using Tomcat, it is possible to have a very productive developer environment.
Here is a sample Embedded Tomcat you can start in a Scala project:

import org.apache.catalina.
import org.apache.catalina.startup.;

object TomcatScalaServer {
  val CATALINAHOME : File = new File("../");
  val WEBAPPS : File = new File(CATALINAHOME,"webapps");
  val ROOT : File = new File(CATALINAHOME,"web");
  val HOSTNAME : String = "localhost";
  val PORT : int 8080;
  def await() {
     whiletrue ) {
          try {
            System.out.println("sleeping 100s");
              Thread.sleep100000 );
          catch {
            case ie : InterruptedException =>;
  def start() {
    val server = new Embedded();

    val engine = server.createEngine();

    val host = server.createHost(HOSTNAME, WEBAPPS.getAbsolutePath());

    val context = server.createContext("", ROOT.getAbsolutePath());


    val http = server.createConnector(HOSTNAME, PORT, false);

  def main(args: Array[String]) {


Here is a sample Scala Servlet outputing html directly. This is a simple example, but it shows something important. With Scala, the view layer can just be regular scala classes. There is no need for JSP or other templating languages as Scala already embbeds XML very nicely. By using the reloadable feature of Tomcat (there are also other pure Scala ways) and Eclipse autocompile, changes are instantanously taken in account.
import javax.servlet.http.;

class ScalaServlet extends HttpServlet {

  override def init() {
  override def doGet(request : HttpServletRequest , response : HttpServletResponse{
    service(request, response)
  override def service(req: HttpServletRequest,resp: HttpServletResponse) { 
    val pw = resp.getWriter();
    var output = <html>
    <head><title>Scala Servlet Test</title></head>
      <h1>Hello World!</h1>

Now I am eagerly waiting for improvements in the Eclipse Scala plugin (Organise imports, class navigation).

comments powered by Disqus
Tweet Submit to reddit
© 2006-16 Fabien Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License.