Getting Started with Play Framework: Building Your First Web Application in Scala and Java
The Play Framework is a powerful, lightweight web framework designed for building scalable and reactive applications. Known for its developer-friendly features like hot reloading and a streamlined development process, Play supports both Scala and Java, making it versatile for teams with different language preferences. Whether you’re crafting REST APIs, dynamic web apps, or microservices, Play’s simplicity and robust ecosystem make it a go-to choice.
In this guide, we’ll walk you through the steps to get started with Play Framework: installing it, setting up a new project, and building a simple web application with examples in both Scala and Java. By the end, you’ll have a running app and a solid foundation to explore Play further. Let’s dive in!
What is Play Framework?
Before we jump into the steps, let’s briefly cover what Play brings to the table. Play is an open-source framework built on reactive principles, leveraging the Akka toolkit for concurrency and scalability. It follows an MVC (Model-View-Controller) architecture and integrates seamlessly with tools like sbt (Scala Build Tool) for project management. Key features include:
- Hot Reloading: Edit code and see changes instantly without restarting the server.
- Stateless Design: Ideal for building scalable, cloud-native apps.
- Language Flexibility: Full support for both Scala (its native language) and Java.
- Built-In Tools: Comes with a testing framework, routing system, and more.
Whether you’re a Scala enthusiast or a Java veteran, Play adapts to your workflow. Now, let’s get it up and running.
Step 1: Prerequisites
To follow this guide, ensure you have the following installed:
- Java Development Kit (JDK): Play requires JDK 8 or later. Download it from Oracle or use an OpenJDK distribution like Adoptium. Verify with:
java -version
- sbt (Scala Build Tool): Play uses sbt for Scala projects. Install it via the official sbt site or a package manager (e.g., Homebrew on macOS:
brew install sbt
). Check the version:sbt --version
- IDE (Optional): IntelliJ IDEA or VS Code with Scala/Metals plugins enhances the experience, but a text editor works too.
With these in place, you’re ready to install Play.
Step 2: Installing Play Framework
Play doesn’t require a separate installation—instead, you use sbt to bootstrap a new project with Play dependencies. However, you’ll need to set up a Play starter project to get going. Here’s how:
- Create a Directory: Make a new folder for your project:
mkdir play-demo cd play-demo
- Use sbt to Start: Run the following command to create a new Play project:
sbt new playframework/play-scala-seed.g8
- This uses a Giter8 template (
play-scala-seed.g8
) for Scala. For Java, useplayframework/play-java-seed.g8
instead. - You’ll be prompted for a project name (e.g.,
play-demo
). Hit Enter to accept defaults or customize as needed.
- This uses a Giter8 template (
- Verify Setup: Navigate into the generated folder (e.g.,
cd play-demo
) and run:
After downloading dependencies, Play starts a server atsbt run
http://localhost:9000
. Open your browser to see the welcome page.
That’s it! Play is now installed and running a skeleton app. Next, we’ll create a fresh project and customize it.
Step 3: Creating a New Play Project
Let’s set up two projects—one in Scala, one in Java—to explore both flavors. We’ll use sbt’s templates for consistency.
Scala Project
- Generate the Project:
sbt new playframework/play-scala-seed.g8 --name=scala-webapp cd scala-webapp
- Explore the Structure:
app/controllers/HomeController.scala
: Handles HTTP requests.app/views/index.scala.html
: The default view template.conf/routes
: Defines URL routing.build.sbt
: Project configuration and dependencies.
- Run It:
Visitsbt run
http://localhost:9000
to see the default "Welcome to Play" page.
Java Project
- Generate the Project:
sbt new playframework/play-java-seed.g8 --name=java-webapp cd java-webapp
- Explore the Structure:
app/controllers/HomeController.java
: The main controller.app/views/index.scala.html
: A Twirl template (Play uses Scala-based templates even for Java).conf/routes
: Routing configuration.build.sbt
: Project settings.
- Run It:
Opensbt run
http://localhost:9000
to confirm it’s working.
Both projects are now ready. Let’s build a simple web app next.
Step 4: Building a Simple Web Application
We’ll create a basic app that displays a greeting based on a user’s input (e.g., "Hello, [Name]!"). We’ll implement it in both Scala and Java to highlight the differences.
Scala Version
- Update the Controller (
app/controllers/HomeController.scala
):package controllers import javax.inject._ import play.api.mvc._ @Singleton class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController { def index() = Action { implicit request: Request[AnyContent] => Ok(views.html.index("Welcome to Play!")) } def greet(name: String) = Action { implicit request: Request[AnyContent] => Ok(views.html.index(s"Hello, $name!")) } }
- Adds a
greet
action that takes aname
parameter.
- Adds a
- Update the Routes (
conf/routes
):GET / controllers.HomeController.index GET /greet/:name controllers.HomeController.greet(name: String)
- Modify the View (
app/views/index.scala.html
):@(message: String) @main("Scala Web App") { <h1>@message</h1> <p>Enter your name in the URL, e.g., <a href="/greet/Alice">/greet/Alice</a></p> }
- Uses Twirl syntax (
@
) to inject the message.
- Uses Twirl syntax (
- Test It:
- Run
sbt run
. - Visit
http://localhost:9000/greet/Alice
to see "Hello, Alice!"
- Run
Java Version
- Update the Controller (
app/controllers/HomeController.java
):package controllers; import play.mvc.*; public class HomeController extends Controller { public Result index() { return ok(views.html.index.render("Welcome to Play!")); } public Result greet(String name) { return ok(views.html.index.render("Hello, " + name + "!")); } }
- Adds a
greet
method with aname
parameter.
- Adds a
- Update the Routes (
conf/routes
):GET / controllers.HomeController.index GET /greet/:name controllers.HomeController.greet(name)
- Modify the View (
app/views/index.scala.html
):@(message: String) @main("Java Web App") { <h1>@message</h1> <p>Enter your name in the URL, e.g., <a href="/greet/Bob">/greet/Bob</a></p> }
- Test It:
- Run
sbt run
. - Visit
http://localhost:9000/greet/Bob
to see "Hello, Bob!"
- Run
Key Differences
- Scala: Leverages functional programming and concise syntax (e.g., implicits, interpolated strings).
- Java: More verbose but familiar to Java developers, with traditional OOP patterns.
- Views: Both use Twirl templates, which are Scala-based but work seamlessly with Java.
Step 5: Running and Debugging
Play’s hot reloading means changes to code or templates reflect instantly during sbt run
. To debug:
- Logs: Check
logs/application.log
for errors. - Console: sbt’s output shows compilation issues or runtime exceptions.
- IDE: Use IntelliJ’s Play support for breakpoints and step-through debugging.
To stop the server, press Ctrl+D
or Ctrl+C
in the terminal.
Next Steps
You’ve now built a simple Play app in both Scala and Java! Here’s how to level up:
- Add a Form: Use Play’s form handling to accept user input via a POST request.
- Database Integration: Connect to a database with Play’s JDBC or an ORM like Ebean (Java) or Slick (Scala).
- Deploy: Package your app with
sbt dist
and deploy to a server or cloud platform like Heroku.
Explore the official Play documentation for deeper dives into routing, testing, and reactive programming.
Final Thoughts
Play Framework offers a refreshing approach to web development, blending simplicity with power. Its dual-language support means you can start with Java’s familiarity or dive into Scala’s elegance—either way, you’ll enjoy a productive workflow backed by a vibrant community. Setting up Play is straightforward with sbt, and building your first app is just the beginning of what’s possible.
So, grab your IDE, fire up a terminal, and start experimenting with Play today. Which language will you choose—Scala or Java? Let me know in the comments, along with any cool Play projects you build!