<commit_before># Your first Play application

Let’s write a simple task list application with Play and deploy it to the cloud. This is a very small example which can be managed in a few hours.

## Prerequisites

First of all, make sure that you have a [[working Play installation|Installing]]. You only need Java (version 6 minimum), and to unzip the Play binary package to start; everything is included.

As we will use the command line a lot, it’s better to use a Unix-like OS. If you run a Windows system, it will also work fine; you’ll just have to type a few commands in the command prompt.

You will of course need a text editor. If you are used-to a fully-featured Java IDE, such as Eclipse or IntelliJ, you can of course use it. However, with Play you can have fun working with a simple text editor like TextMate, Emacs or vi. This is because the framework manages compilation and the deployment process itself.

> **Note:** Read more about [[Setting-up your preferred IDE | IDE]].

## Project creation

Now that Play is correctly installed, it’s time to create the new application. Creating a Play application is pretty easy and fully managed by the Play command line utility. This encourages a standard project layout across all Play applications.

Open a new command line and enter:

```
$ play new todolist
```

The Play tool will ask you a few questions. Choose to create a **simple Java application** project template.

[[images/new.png]]

The `play new todolist` command creates a new directory `todolist/` and populates it with a series of files and directories. The most important are as follows.

* `app/` contains the application’s core, split between models, controllers and views directories. This is the directory where .java source files live.
* `conf/` contains all the application’s configuration files, especially the main `application.conf` file, the `routes` definition files and the `messages` files used for internationalization.
* `project/` contains the build scripts. The build system is based on sbt. But a new play application comes with a default build script that will just works fine for our application.
* `public/` contains all the publicly available resources, which includes JavaScript, stylesheets and images directories.
* `test/` contains all the application tests. Tests can be written as JUnit tests.

> Because Play uses UTF-8 as the single encoding, it’s very important that all text files hosted in these directories use this encoding. Make sure to configure your text editor accordingly. In the windows system the editor configuration must be ANSI encode
> **Note:** Read more about [[Anatomy of a Play application | Anatomy]].

## Using the Play console

Once you have an application created, you can run the Play console. Go to the new `todolist/` directory and run:

```
$ play
```

This launches the Play console. There are several things you can do from the Play console, but let’s start by running the application. From the console prompt, type `run`:

```
[todolist] $ run
```

[[images/run.png]]

Now the application is running in development mode. Open a browser at <http://localhost:9000/>:

[[images/welcome.png]]

> **Note:** Read more about [[The Play Console|PlayConsole]].

## Overview

Let’s see how the new application can display this page.

The main entry point of your application is the `conf/routes` file. This file defines all of the application’s accessible URLs. If you open the generated routes file you will see this first _route_:

```
GET	/       controllers.Application.index()
```

That simply tells Play that when the web server receives a GET request for the `/` path, it must call the `controllers.Application.index()` method. 

Let’s see what the `controllers.Application.index` method looks like. Open the `todolist/app/controllers/Application.java` source file:

@[controller-application](code/javaguide/todo/initial/controllers/Application.java)

You see that `controllers.Application.index()` returns a `Result`. All action methods must return a `Result`, which represents the HTTP response to send back to the web browser.

> **Note:** Read more about [[Actions|JavaActions]].

Here, the action returns a **200 OK** response with a HTML response body. The HTML content is provided by a template. Play templates are compiled to standard Java methods, here as `views.html.index.render(String message)`.

This template is defined in the `app/views/index.scala.html` source file:

@[index-welcome](code/javaguide/todo/initial/views/index.scala.html)

The first line defines the function signature. Here it takes a single `String` parameter. Then the template content mixes HTML (or any text-based language) with Scala statements. The Scala statements start with the special `@` character.

> **Note:** Don’t worry about the template engine using Scala as its expression language. This is not a problem for a Java developer, and you can almost use it as if the language was Java. We explain the templating system in a bit more deatil below.

## Development work-flow

Now let’s make some modifications to the new application. In the `Application.java` change the content of the response:

```
public static Result index() {
  return ok("Hello world");
}
```

With this change, the `index` action will now respond with a simple `text/plain` **Hello world** response. To see this change, just refresh the home page in your browser:<commit_msg>fixed typo<commit_after># Your first Play application

Let’s write a simple task list application with Play and deploy it to the cloud. This is a very small example which can be managed in a few hours.

## Prerequisites

First of all, make sure that you have a [[working Play installation|Installing]]. You only need Java (version 6 minimum), and to unzip the Play binary package to start; everything is included.

As we will use the command line a lot, it’s better to use a Unix-like OS. If you run a Windows system, it will also work fine; you’ll just have to type a few commands in the command prompt.

You will of course need a text editor. If you are used-to a fully-featured Java IDE, such as Eclipse or IntelliJ, you can of course use it. However, with Play you can have fun working with a simple text editor like TextMate, Emacs or vi. This is because the framework manages compilation and the deployment process itself.

> **Note:** Read more about [[Setting-up your preferred IDE | IDE]].

## Project creation

Now that Play is correctly installed, it’s time to create the new application. Creating a Play application is pretty easy and fully managed by the Play command line utility. This encourages a standard project layout across all Play applications.

Open a new command line and enter:

```
$ play new todolist
```

The Play tool will ask you a few questions. Choose to create a **simple Java application** project template.

[[images/new.png]]

The `play new todolist` command creates a new directory `todolist/` and populates it with a series of files and directories. The most important are as follows.

* `app/` contains the application’s core, split between models, controllers and views directories. This is the directory where .java source files live.
* `conf/` contains all the application’s configuration files, especially the main `application.conf` file, the `routes` definition files and the `messages` files used for internationalization.
* `project/` contains the build scripts. The build system is based on sbt. But a new play application comes with a default build script that will just works fine for our application.
* `public/` contains all the publicly available resources, which includes JavaScript, stylesheets and images directories.
* `test/` contains all the application tests. Tests can be written as JUnit tests.

> Because Play uses UTF-8 as the single encoding, it’s very important that all text files hosted in these directories use this encoding. Make sure to configure your text editor accordingly. In the windows system the editor configuration must be ANSI encode
> **Note:** Read more about [[Anatomy of a Play application | Anatomy]].

## Using the Play console

Once you have an application created, you can run the Play console. Go to the new `todolist/` directory and run:

```
$ play
```

This launches the Play console. There are several things you can do from the Play console, but let’s start by running the application. From the console prompt, type `run`:

```
[todolist] $ run
```

[[images/run.png]]

Now the application is running in development mode. Open a browser at <http://localhost:9000/>:

[[images/welcome.png]]

> **Note:** Read more about [[The Play Console|PlayConsole]].

## Overview

Let’s see how the new application can display this page.

The main entry point of your application is the `conf/routes` file. This file defines all of the application’s accessible URLs. If you open the generated routes file you will see this first _route_:

```
GET	/       controllers.Application.index()
```

That simply tells Play that when the web server receives a GET request for the `/` path, it must call the `controllers.Application.index()` method. 

Let’s see what the `controllers.Application.index` method looks like. Open the `todolist/app/controllers/Application.java` source file:

@[controller-application](code/javaguide/todo/initial/controllers/Application.java)

You see that `controllers.Application.index()` returns a `Result`. All action methods must return a `Result`, which represents the HTTP response to send back to the web browser.

> **Note:** Read more about [[Actions|JavaActions]].

Here, the action returns a **200 OK** response with a HTML response body. The HTML content is provided by a template. Play templates are compiled to standard Java methods, here as `views.html.index.render(String message)`.

This template is defined in the `app/views/index.scala.html` source file:

@[index-welcome](code/javaguide/todo/initial/views/index.scala.html)

The first line defines the function signature. Here it takes a single `String` parameter. Then the template content mixes HTML (or any text-based language) with Scala statements. The Scala statements start with the special `@` character.

> **Note:** Don’t worry about the template engine using Scala as its expression language. This is not a problem for a Java developer, and you can almost use it as if the language was Java. We explain the templating system in a bit more detail below.

## Development work-flow

Now let’s make some modifications to the new application. In the `Application.java` change the content of the response:

```
public static Result index() {
  return ok("Hello world");
}
```

With this change, the `index` action will now respond with a simple `text/plain` **Hello world** response. To see this change, just refresh the home page in your browser:
{'fraction_non_alphanumeric': 0.07243572021011889, 'fraction_numerical': 0.0016588332872546309, 'mean_word_length': 4.453266331658291, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3139873', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2686, 'n_words': 1578}