Scala Servlet development

birdsnest.jpg

Scala sources compile to class files that can run on JVM. So there is no doubt that most of Java EE development should be possible in Scala. Here is a simple servlet written in Scala.


package net.aseem.tryingoutscala.web
import javax.servlet.http._

class Payroll extends HttpServlet {
override def doGet(request: HttpServletRequest, response: HttpServletResponse) =
response.getWriter().println("Hello World")
}

What is yet not available is a reasonable IDE for Scala based Java EE application development. There is a Scala plugin for Eclipse that works great for Scala applications. So I went ahead and used it along with another plugin to get an effective Java EE development environment for Scala. I used Oracle Enterprise Pack for Eclipse (formerly know as WLS Tools) along with Scala Plugin for Eclipse to develop and deploy aforementioned Scala servlet. I installed these plugins on Eclipse IDE for Java EE Developers (version Ganymede) and tested the servlet on WebLogic Server 10g Release 3 (10.3).

Scala project
I created a Scala project called TryingOutScala (File > New > Project > Scala Wizards > Scala Project) an added Servlet 2.5 spec jar to it's build path (Project > Properties > Java Build Path > Libraries > Add External JARs). I added the jar from WLS installation at BEA_HOME/modules/javax.servlet_1.0.0.0_2-5.jar. Then I created the above mentioned Scala servlet in this project (File > New > Other > Scala Wizards > Scala Class).

Web project
Next I created a dynamic web project called Payroll (File > New > Project > Web > Dynamic Web Project) and added TryingOutScala project to it's Java EE module dependencies (Project > Properties > Java EE Module Dependencies). To web.xml of this project (located in WebContent/WEB-INF) I added the servlet from the Scala project.


<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>net.aseem.tryingoutscala.web.Payroll</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Scala Library
Our Scala servlet was now ready to be deployed, except Scala compiled classes need at least one library in classpath at runtime: scala-library.jar. For my installation, this file was available at /usr/share/java/scala-library.jar. Though packaging this library inside the web application would have worked, it would have considerably increased the size of the application. So I deployed it as a shared application library using the following command:

java weblogic.Deployer -username *** -password *** -library -deploy -name scala-library /usr/share/java/scala-library.jar

This library could also have been deployed using the console.

I added the following two lines to the top of manifest file of the web project (WebContent/META-INF/MANIFEST.MF) to ensure that the deployed library is available to the deployed application:

Extension-List: scalalib
scalalib-Extension-Name: scala-library

Deployment & Testing
To deploy the Scala servlet, I first added my local WLS installation to the available runtimes for the IDE (Window > Preferences > Server > Runtime Environments). After this was done, Payroll project was deployed by simply running it on the server that had just been added (Run > Run > Run on Server).

Comments:

Aseem, I'm glad to see that you are also exploring Scala, I've found it to be very intriguing. Have you seen customers or Oracle technical resources asking about this yet? The idea of having a functional language with strong typing, all running on the JVM seems very compelling. At this point I wonder if Scala will stay in the "just experimenting" or "theoretical" stages, or if Scala could get more mainstream adoption in typical Enterprises. I wrote up a couple entries previously discussing the multitude of language choices and my first experiences with Scala. http://blogs.oracle.com/jamesbayer/2008/02/paparazzi_for_programming_lang.html http://blogs.oracle.com/jamesbayer/2008/02/scala_development_with_eclipse.html I read Daniel Spiewak's blog regularly to learn more, but I have yet to apply Scala in a real-world situation. http://www.codecommit.com/blog/

Posted by James Bayer on August 05, 2008 at 11:23 AM PDT #

Hey James, I like the fact that Scala code simply runs on the JVM. It makes trying it out easy. I don't know, but some of our customers might already be using it on top of WLS. Of course it's all because Scala compiler compiles to class files that JVM can simply run, be it any Java app server. So it was with that frame of mind, I wanted to explore some customer use cases. The other side reason was to try out a functional language.

Posted by Aseem Bajaj on August 06, 2008 at 04:40 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Posts about application development

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today