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.
No comments:
Post a Comment