| Software Secret Weapons™ |
Run Scala in Apache Tomcat in 10 minutes posted by Pavel Simakov on 2008-03-21 02:54:52 under Lambda
|
|||
The ContextI am researching Scala because it might just be the next big things. My specific interests are in using Scala to replace the aging parts of an existing large Java systems with their Scala alternative and hopefully more elegant implementations. There are not enough examples out there today on interoperability with Java and we need more good examples. So I decided to write some classical Java code using Scala. For this exercise I implement Servlet Filter (javax.servlet.Filter) in both Java and Scala side by side. The dozen lines of code below is sufficient to fully Scala enable your favorite Java application server! This is not a rocket science, but the complete example never hurts. It helped me to get my feet wet. I hope it shows you the availability and non intrusiveness of Scala and gets you excited. You can download complete Eclipse 3.1.0 Servlet Filter in Scala project that has both Java and Scala source code for servlet filters.Get the project, get Tomcat 5.5.26, start Tomcat, run build.xml, drop created classic-scala-filter.war and classic-java-filter.war to the %CATALINA_HOME%/webapps folder and access the servlet filters: java (via http://localhost:8080/classic-java-filter/) and scala (via http://localhost:8080/classic-scala-filter/). Servlet Filter in Java
package com.oy.shared.scample.java;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ClassicJavaFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("> ClassicJavaFilter: init()");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws java.io.IOException, ServletException {
System.out.println("> ClassicJavaFilter: doFilter()");
response.getWriter().write("> The time now is " + new Date());
}
public void destroy(){
System.out.println("> ClassicJavaFilter: destroy()");
}
}
Servlet Filter in ScalaNote _root_.java.io.IOException and _root_.java.util.Date. The code does not compile unless I put _root_ in there. The Scala package import seem to be quite sophisticated, but without _root_ you get a compile error: "value io is not a member of package com.oy.shared.scample.java".
package com.oy.shared.scample.java
import _root_.java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
class ClassicScalaFilter extends Object with Filter {
@throws(classOf[ServletException])
def init(filterConfig: FilterConfig) : Unit = {
println("> ClassicScalaFilter: init()");
}
@throws(classOf[_root_.java.io.IOException])
@throws(classOf[ServletException])
def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) : Unit = {
println("> ClassicScalaFilter: doFilter()");
response.getWriter().write("> The time now is " + new Date);
}
def destroy() : Unit = {
println("> ClassicScalaFilter: destroy()");
}
}
Compiling Scala from AntBeware of a small issue here with classpath and taskdef.
<target name="compile-scala">
<taskdef resource="scala/tools/ant/antlib.xml">
<classpath>
<pathelement location="${scala.home}/lib/scala-compiler.jar"/>
<pathelement location="${scala.home}/lib/scala-library.jar"/>
</classpath>
</taskdef>
<path id="cp-scala">
<pathelement location="${scala.home}/lib/scala-compiler.jar"/>
<pathelement location="${scala.home}/lib/scala-library.jar"/>
<pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>
</path>
<mkdir dir="build/scala"/>
<scalac srcdir="src-scala" destdir="build/scala" classpathref="cp-scala" force="changed">
<include name="**/*.scala"/>
</scalac>
</target>
The Final WordScala is absolutely brilliant. If runtime penalties are bearable - expect wide adoption everywhere. Comments (2) Leave a comment |
|
|||
| Copyright © 2004-2007 by Pavel Simakov |
|
Comment by Gabriel C. — March 22, 2008 @ 12:44 pm
Scala is very promissing and I think is brilliant too :).
I ran a simple experiment on how easy is to call Scala code from Java in Eclipse (and you can have code-completion even!), I wrote it on my blog.
Comment by Daniel SPiewak — March 23, 2008 @ 4:43 pm
Scala really is incredible. As an aside, the runtime penalties are practically nil. In fact, for some things (such as ray tracing), Scala is *faster* than straight-up Java.
BTW, here’s your example a bit more Scalafied:
package com.oy.shared.scample.java
import java.util.Date
import javax.servlet.{Filter, FilterChain, FilterConfig, ServletRequest, ServletResponse}
class ClassicScalaFilter extends Filter {
def init(filterConfig:FilterConfig) = println(”> ClassicScalaFilter: init()”)
def doFilter(request:ServletRequest, response:ServletResponse, chain:FilterChain) {
println(”> ClassicScalaFilter: doFilter()”)
response.getWriter().write(”> The time now is ” + new Date())
}
def destroy() = println(”> ClassicScalaFilter: destroy()”)
}