DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. I've had enough of running Scala in a terminal, let's try with a web application

I've had enough of running Scala in a terminal, let's try with a web application

Giorgio Sironi user avatar by
Giorgio Sironi
·
Oct. 27, 11 · Interview
Like (0)
Save
Tweet
Share
14.79K Views

Join the DZone community and get the full member experience.

Join For Free

I 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.

Web application Scala (programming language) terminal

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Practical Example of Using CSS Layer
  • Using GPT-3 in Our Applications
  • 3 Main Pillars in ReactJS
  • 7 Most Sought-After Front-End Frameworks for Web Developers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: