Tools Used
- Maven 2.2.1
- Netbeans 6.8 with appropriate Scala plugin
Creating the Project
$ mvn archetype:generate
At the time of creating this app, option 30 was the scala-archetype-simple. Select this option and give other details. A Scala Maven project will be created.
Making the Scala Maven Project a Web Project
- Create the directory src/main/webapp/WEB-INF.
- Create a web.xml and place inside src/main/webapp/WEB-INF.
- Edit pom.xml and add <packaging>war</packaging> immediately after the <version> elements.
- In pom.xml, just before the closing </build> element, add <finalName>${artifactId}</finalName>. This will ensure the final WAR file does not have version in its name.
- Add the following dependencies in pom.xml:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
Now you have your Scala webapp ready to compile!
Writing the code
Now that you have a buildable module, it is now time to write your code. Open this module in Netbeans. Write the servlet:
package org.wiztools
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
class ServletIndex extends HttpServlet {
override def doGet(req: HttpServletRequest, res: HttpServletResponse) {
req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").include(req, res);
}
}
Add the servlet detail to your web.xml:
<servlet>
<servlet-name>ServletIndex</servlet-name>
<servlet-class>org.wiztools.ServletIndex</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletIndex</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
Now write your index.jsp page and place it in src/main/webapp/WEB-INF/jsp directory:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Scala First Servlet</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Deploying
$ mvn package
Copy the generated WAR file to your application server.
|