Web applications with the Play framework

DZone 's Guide to

Web applications with the Play framework

· Java Zone ·
Free Resource
The Play framework defines itself as a clean alternative to bloated enterprise Java stacks. I like this definition and after having been advised to try it out, I set out some Pomodoros for coding a pair of Hello, World applications in Play.

What the docs say

The first impressions from the documentation is very good: Play is a full stack framework already integrate with Hibernate, memcached and other tools. This is not necessarily the best choice for the long term, but it's a bliss for a PHP programmer who wants to experience a new platform without debugging exceptions (as long as the integration works well.)

The framework is written in pure Java, so it can interface with any JAR. However it also supports Scala code natively, and that's why I chose it for my experiments. Developing Scala web applications from scratch requires to setup boring scripts for compiling sources and deploying the right JAR files. Again, the best solution is not necessarily to outsource everything to a framework, but in my case it's the path of least resistance.

Play is also a shared nothing system, like PHP applications. It scales automatically by multiplying the instances (if I understood correctly, without relying on a single application server.)

Hello world in Java

Download and unzip Play where you want. The play command line utility will be used to create new projects, and needs to be sylinked on the our PATH.

A command line utility is better than Eclipse automation in my opinion: it's always possible to reuse and integrate these utilities in scripts, while it is not usually so with the IDE's features. Moreover I would like to use a framework no matter which is my IDE: when I worked with SMILA I was forced to write code in Eclipse.

The framework manages the compilation and the deployment process itself. I couldn't say it better.

Creating an application and starting it is really easy, and produces no exceptions:

play new helloworld
play run helloworld

If you look at the helloworld/ folder, you'll see no .class files. The source code is compiled by Play after each modification, as long as it is running. Initially, an empty application will display an help page.

Basic features

Routes are easy to add, although advanced syntax requires a bit of studying. However, routes are really integrated and you will never write an URL yourself, but just leverage referr to urls as their corresponding action with *class.method*.

Controller classes are static, which is a bit weird but not very important since I never write much code in them. They accept HTTP parameters as method parameters (like Symfony2):

    public static void sayHello(@Required String myName) {
        // call this with url?myName=...

As you can see, Play provides annotations for input validation, but does not redirect away from the action by default: you have control on what happens after a failed validation.

The templates are rendered by passing them their data dependencies; Play features a simple language for templating, which supports inheritance to provide layouts.

The Hello world tutorial for Java applications contains some screenshots and all the code you will want to check out.

Writing Scala code

Scala support is provided by an additional module, which can be installed directly via the command line:

play install scala
play new helloworldscala --with scala
play run

The classes of this application will be written in Scala:

object Application extends Controller {
    import views.Application._

    def index = {
        html.index("Your Scala application is ready!")


After adding a bit of logic in the controller, it looks like this:

def sayHello = {
    val myName = params.get("myName")
    if(myName == "") {
        flash += ("error" -> "Oops, please enter your name!")
    } else {

The Scala module features a different template engine: template have types such as (String) => Html in the case of a single parameter. You won't extend a template to build a layout, but include it and passing it an Html argument:

@main(title) {
    Hello, world

This template will print Hello, world inside the layout, which is built in the main template. External templates are just other objects where you can pass in parameters like the title of the page.

The Scala hello world example provides the same application as the Java hello world, but implemented with Scala code. I walked through it and again I reached the end without exceptions, which is not easy for a PHP developer in the JVM world. It probably means the framework is mature enough for anyone to use, and the precision the documentation is written with is really beneficial for the naive developer. Even exceptions are well-displayed and focused:


Play will definitely be my choice for JVM-based web applications. It's easy to work with and promises not to be a drain on productivity. It is certainly the fastest way to get Scala code accessible via HTTP: you don't even need an IDE to get started.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}