If you’re like me, you heard about this language on the Java Posse Pod-cast (link), and you’re just now trying to figure out what all the hubbub is about. Well, Scala is functional, declaration, strongly typed (although it doesn’t feel like it), and most importantly, a JVM language. it compiles to Java classes, and with the inclusion of a single library, you can work Scala quite easily into your web application, either by design or postfixed.
Aside from ease of integration, Scala also offers many functional concepts: first-class functions, curried functions, partially applied functions, and in simple cases, tail recursion! Object-Oriented purists can continue to work as they would in Java, but those of us willing to dabble in the dark-arts can do things like this:
class MyApplication extends Application {
println( "Hello, World!" )
}
See what’s happening? The closure between the braces is passed as a constructor argument to MyApplication, which then executes the function. It’s not that much of a convenience, but for small applications, public static void main( String[] args ) is just annoying.
Tomcat
Remember how I said Scala could run under Tomcat as easily as Java? Well, I setup a servlet on my server to proxy file downloads. Try this:
http://stefankendall.com:8080/download?url=http://stefankendall.com/files/test.txt
It’s not much to see, but the code is pretty simple!
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
import javax.servlet.ServletOutputStream
import java.net.URL
import java.io.{BufferedInputStream, PrintStream, StringWriter, PrintWriter }
class ProxyDownloadServlet extends HttpServlet
{
override def doGet( request: HttpServletRequest, response: HttpServletResponse )
{
//get the file url to download from the GET request parameter
val fileUrl = request.getParameter( "url" );
//if no url was provided, write that to the user
if( fileUrl == null )
{
response.getWriter().println( "<html><body><p>No download url!</p></body></html>" )
return
}
try
{
//download the file
val inputStream = new BufferedInputStream( new URL( fileUrl ).openStream() )
//set the content type of the response so the
//user will be prompted to download
response.addHeader( "Content-Type","application/octet-stream");
//get the output stream
val outputStream = response.getOutputStream();
//data buffer
var buffer = new Array[byte](4 * 1024)
var bytesRead = inputStream.read( buffer )
while( bytesRead != -1 )
{
outputStream.write( buffer, 0, bytesRead )
bytesRead = inputStream.read( buffer )
}
}
catch
{
case e => {
println( "Attempted to download:" )
println( fileUrl )
e.printStackTrace()
response.setContentType("text/html")
outputStream.println( "<html><body><p>Bad file url!</p>" )
outputStream.println( "<p>File: " )
outputStream.println( fileUrl );
outputStream.println( "</p>" )
outputStream.println( "</body></html>" )
}
}
finally
{
inputStream.close()
outputStream.close()
}
}
}
I had some trouble getting things like String concatenation working under Tomcat (Yeah, I know.), and the tools for Scala are rather weak right now (at least the eclipse plugin), but it’s definitely not Java! There’s probably a more Scala way to do this, but that just illustrates the point: you can use as much, or little, Java-isms as you see fit. Remember that this compiles to .class files, so it can mingle with other Java servlets as easily as other Java could.