Saturday, May 7, 2011

Learning Scala : Hello World Web App with SBT

Creating Project:-

mkdir HelloWorld
cd HelloWorld

You will be prompted to create new project, type 'y' and enter project name, organization, version etc.:-

Name: HelloWorld
Organization: QQ Enterprise
Version [1.0]:
Scala version [2.8.1]:
sbt version [0.7.5]:

Required Scala libraries will be downloaded automatically, make sure you are connected to internet.

Create WebAppBuild.scala build in project/build/ (create build directory if it's not there):-

import sbt._
class WebAppBuild(info: ProjectInfo) extends DefaultWebProject(info) {
val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test"
val servletApi = "javax.servlet" % "servlet-api" % "2.5"

In the SBT console, enter:-


Create HelloWorldServlet.scala in src/main/scala directory:-

import javax.servlet.http._
class HelloWorldServlet extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
resp.getWriter().print("Hello World!")

Create webapp directory in src/main, and consequently create WEB-INF directory and web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"



In SBT, run:-


Open your web browser and go to http://localhost:8080, you should see the Hello World! message being print out from your simple servlet written in Scala.

No comments:

Post a Comment