How to build an app in the Play Framework

Using the Play Framework is a reliable and effective method for creating web apps in Java or Scala. Play Framework offers developers a successful and pleasurable development experience with its reactive and lightweight architecture.

You can use Play's extensive feature set, which includes integrated support for RESTful APIs, real-time updates, asynchronous programming, and a strong community of plugins and modules. Create scalable, responsive, and extremely fast apps with the Play Framework, whether starting a new project or updating an existing one.

Building an application in Play

To build a Play Framework application using Java 11, you can follow these steps.

  • Install Java 11

sudo apt update
sudo apt install openjdk-11-jdk
Installinng Java 11
  • Verify Java installation

java --version
Confirming Java version

The output should indicate that Java 11 is installed.

  • Download and install scala build tool (sbt)

curl -L -o sbt.deb https://repo.scala-sbt.org/scalasbt/debian/sbt-1.5.5.deb
sudo dpkg -i sbt.deb
sudo apt update
sudo apt install sbt
Instructions to download and install sbt
  • Verify sbt installation

sbt --version
Verifying the sbt version installed

The output should indicate the sbt version that is installed.

  • Create a new Play Framework application

sbt new playframework/play-java-seed.g8 --name=my-play-app
Creating a Play Framework application

You can replace my-play-app with the desired name for your Play application.

  • Change into the project directory

cd my-play-app
Changing the working directory
  • Run the Play application

sbt run
Running the application

This command will start the Play development server, and you should see an output indicating that the application is running.

Congratulations! You have successfully created and run a Play Framework application using Java 11. You can access the application in your web browser at http://localhost:9000.

Running the application on custom port number

If you don't want the output displayed on this port number, you can run the application at your desired port number by making the following changes.

To specify a custom port for your Play application, follow these steps:

  • Open the application.conf file located in the conf directory.

  • Add or modify the play.http.port property in the file to specify the desired port number. For example:

play.http.port = 9300
Defining custom port number
  • The application is configured to run on port 9300 in the above code. You can replace it with your preferred port number.

  • Save the application.conf file.

  • Run the Play application with the modified port configuration.

Now your Play application will be running on the specified port (e.g., http://localhost:9300). You can access it in your web browser using the custom port number you configured.

You can do this by defining it in your run command

sbt 'run -Dhttp.port=9300'
Defining the custom port in run command

Play application

This runs the application on http://localhost:9300 and displays a "Welcome to Play!" prompt on the server.

Please add your host link to the application.conf file in the list

play.filters.hosts.allowed = ["localhost", "ed-5934822110003200.educative.run"]
Add your host in the filters list

As shown here

package controllers;

import play.mvc.*;

/**
 * This controller contains an action to handle HTTP requests
 * to the application's home page.
 */
public class HomeController extends Controller {

    /**
     * An action that renders an HTML page with a welcome message.
     * The configuration in the <code>routes</code> file means that
     * this method will be called when the application receives a
     * <code>GET</code> request with a path of <code>/</code>.
     */
    public Result index() {
        return ok(views.html.index.render());
    }

}
Running a Play application

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved