I've had enough of running Scala in a terminal, let's try with a web application
Join the DZone community and get the full member experience.
Join For FreeI was checking out Scala as a new programming language to learn, and after remaining positively impressed I wanted to do more than making fake tests pass. Being a web developer by nature, I started trying to solve the first fundamental problem: write a web application in Scala.
Trying out Lift
I thought the simplest solution was to start with a framework: I downloaded Lift, a major Scala web framework. An example for the old-school programmer like me is available (from_mvc), ready for being compiled and run via Maven.
A framework provides you structure
Lift runs on Jetty (an embedded servlet container) for rapid development; since all code compiles to the JVM bytecode as always, the servlet container you can use are the same as for Java web applications.
If we look into the source tree for a Lift application, we discover some interesting places:
src/main/webapp contains static files like HTML, CSS and JavaScript files. Actually, these templates are parsed and widgets are substituted basing on particular attributes:
<span class="lift:TimeNow">??? some time</span>
main/scala/bootstrap/liftweb/Boot.scala is a bootstrap class for configuring options like routes and character encoding.
main/scala/code/snippet contains the Scala source files for pages widgets:
// our snippet object TimeNow { // create a function (NodeSeq => NodeSeq) // that puts the current time into the // body of the incoming Elem def render = "* *" #> now.toString }
Well, it's a bit complicated as any framework: it has a learning curve which I would have to work on. Let's try something simpler. For sure a framework has some interesting tools for compilation and deployment which are already integrated, but it does not let me learn what happens under the hood.
Plain old servlets
The minimum common denominator for web development on the JVM is the Servlet Api. I found an HelloScalaServlet class following this standard and I started putting up a small hello world example, starting from the Java code.
package com.dzone; import javax.servlet.http.{HttpServlet, HttpServletRequest => HSReq, HttpServletResponse => HSResp} class HelloScalaServlet extends HttpServlet { override def doGet(req : HSReq, resp : HSResp) = resp.getWriter().print("<HTML>" + "<HEAD><TITLE>Hello, Scala!</TITLE></HEAD>" + "<BODY>Hello, Scala! This is a servlet.</BODY>" + "</HTML>") }
This servlet needs some configuration to run, such as a simple WEB-INF.xml to configure the route:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>helloWorld</servlet-name> <servlet-class>com.dzone.HelloScalaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloWorld</servlet-name> <url-pattern>/sayHello</url-pattern> </servlet-mapping> </web-app>
The problem is I don't know how to compile this stuff from the cli. scala returns erros because servelet package is not found, and neither scalax works out of the box. I don't know how to deal with compilation errors, but I can do what I always do when I work with Java code: using an IDE.
Let's add some tooling
ScalaIDE is an Eclipse plugin which let us create Scala projects, promising to provide support for the build path and for compilation out of the box. This plugin works on Eclipse 3.6.2 and 3.7, and its installation can be made from inside Eclipse in the Install new software dialog. There's even a video explaining the installation ste-by-step: I am a Vim guy but I have to admit ScalaIDE comes really handy.
After installing this plugin and adding Apache Tomcat v6.0 in the libraries of the project, the compilation went on successfully.
Non-immediate deployment
This was a standard Java deployment: I copied the web.xml into the WEB-INF directory of the web application, and the HelloScalaServlet.class into WEB-INF/classes, complete with its package (so it's actually WEB-INF/classes/com/dzone/HelloScalaServlet.class).
However, there was still a missing piece:
javax.servlet.ServletException: Error allocating a servlet instance ... root cause java.lang.NoClassDefFoundError: scala/ScalaObject
Having compiled this servlet from Scala code, I was missing the basic scala classes in Tomcat's environment.
So I copied my scala-library.jar into /usr/share/tomcat6/lib/, which is Tomcat's libraries folder. After a restart, now I can deploy any servlet derived from Scala code in this instance of Tomcat.
My conclusions
If I were to start a serious web application with Scala, indeed I would look for a framework like Lift to save me time. However, while learning, it's better for me to build my own tools in the fields I'm interested in; for example I could write an Ant-based deployment instead of relying on Eclipse (which can't even run by default Scala projects in Tomcat).
The problem when adopting a new language is usually not in the language itself, but in the lack of tooling. However, Scala has demonstrated that if you come from the Java background you can install an IDE in minutes, and deploy almost as if you were writing Java code.
Opinions expressed by DZone contributors are their own.
Comments