Chee Seng Chua
Posts: 62
Nickname: quai83
Registered: Nov, 2006
Chee Seng Chua is Senior Application Developer for KZEN Solutions Berhad
Learning Scala : Hello World Web App with SBT
Posted: May 7, 2011 7:42 AM
Advertisement
Creating Project:- mkdir HelloWorld cd HelloWorld sbt
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:- reload update
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" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> </web-app>
In SBT, run:- reload jetty-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.
Read: Learning Scala : Hello World Web App with SBT