SlideShare a Scribd company logo
1 of 45
Download to read offline
Business Informatics Group
Institute of Software Technology and Interactive Systems
Vienna University of Technology
Favoritenstraße 9-11/188-3, 1040 Vienna, Austria
phone: +43 (1) 58801-18804 (secretary), fax: +43 (1) 58801-18896
office@big.tuwien.ac.at, www.big.tuwien.ac.at
Play Framework: The Basics
Building scalable web applications based on a non-blocking, stateless
architecture and the Java Persistence API
Philip Langer
Outline
1. Motivation and overview
2. Setup and project structure
3. Controllers and routing
4. Templates with Scala
5. Models and JPA
6. Forms and server-side validation
7. Internationalization
8. Authentication
9. Client state on the server
10.Asynchronous results
Motivation and Overview
3
 Goals of the Play framework
 Stateless web framework
 As opposed to stateful
 Based on an “evented” web server architecture
 As opposed to threaded
 Full-stack web framework
 Including model persistence, template mechanism (view), controller, and testing
 Aims at being
 “developer-friendly” (e.g., hot reload mimicking interpreted languages)
 fully compiled and type safe (even templates and routes)
 Further nice features
 RESTful by default
 Integration of JSON
 Integration of compilers for CoffeeScript, LESS, etc.
 Support for long-living connections (WebSocket, Comet, …)
 …
Motivation and Overview
4
 Stateful versus stateless web frameworks
 Traditional web frameworks are stateful
Server-side state is stored for each client in a session
Web
Server DB
Motivation and Overview
5
 Stateful versus stateless web frameworks
 Traditional web frameworks are stateful
Server-side state is stored for each client in a session
Web
Server DB
Motivation and Overview
6
 Stateful versus stateless web frameworks
 Traditional web frameworks are stateful
Server-side state is stored for each client in a session
Load
Balancer
Web
Server
Web
Server DB
Web
Server
?
Motivation and Overview
7
 Stateful versus stateless web frameworks
 Traditional web frameworks are stateful
Server-side state is stored for each client in a session
Load
Balancer
Web
Server
Web
Server DB
Web
Server
DB
Motivation and Overview
8
 Stateful versus stateless web frameworks
 Recently stateless web frameworks gained popularity
 Web server instances do not store user state (“shared nothing”)
 Stateful client & shared cache (memcached, MongoDB, …)
 Allows to do horizontal scaling (adding/removing web server instances)
Load
Balancer
Web
Server
Web
Server
Web
Server DB
DB
Motivation and Overview
9
 Threaded versus evented web servers
 Thread pool maintains a number of threads
 For each request, a thread is allocated until the request is completed
Load
Balancer
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
DB
Motivation and Overview
10
 Threaded versus evented web servers
 Thread pool maintains a number of threads
 For each request, a thread is allocated until the request is completed
 Thread is occupied but potentially idle
due to long-running requests to other services
Load
Balancer
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
StockService s1 = new StockService();
PricingService s2 = new PricingService();
List<Stock> stocks = s1.requestStocks();
List<Price> prices = new ArrayList<long>();
for (Stock stock : stocks) {
long price = s2.getPrice(stock.getId());
prices.add(price);
}
// ...
renderResponse(response, prices);
}
DB
Motivation and Overview
11
 Threaded versus evented web servers
 Thread pool maintains a number of threads
 For each request, a thread is allocated until the request is completed
 Thread is occupied but potentially idle
due to long-running requests to other services
Load
Balancer
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
When latency goes up here,
threads are all occupied soon
 new requests are queued.
DB
Motivation and Overview
12
 Threaded versus evented web servers
 Thread pool maintains a number of threads
 For each request, a thread is allocated until the request is completed
 Thread is occupied but potentially idle
due to long-running requests to other services
Load
Balancer
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Latency goes up here too,
threads get all occupied
 new requests are queued too.
DB
Motivation and Overview
13
 Threaded versus evented web servers
 Thread pool maintains a number of threads
 For each request, a thread is allocated until the request is completed
 Thread is occupied but potentially idle
due to long-running requests to other services
Load
Balancer
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
Web
Server
same here…
and here…
and here…
and here…
and here…
Motivation and Overview
14
 Threaded versus evented web servers
 Play is based on Netty
An event-driven client-server framework
 Play is based on Akka and the Promise/Future API
Software library for building concurrent applications
 For each CPU core, there is one thread/process
 Enables asynchronous, non-blocking server requests/responses
 Thread is occupied only, if there is something to do
 No sensitivity to downstream latency
Motivation and Overview
15
 Full-stack framework
 Model-view-controller architecture
 Model and controller can be realized with Java or Scala
 View is realized with a template syntax and Scala
 Model persistence is based on Ebean by default
 But any other persistence mechanism can be used too
 We will use the Java Persistence API instead
 Play integrates unit, integration, and system testing tools
 JUnit
 Dedicated APIs to call controllers and views
 Selenium WebDriver and FluentLenium
Motivation and Overview
16
 Developer friendliness
 Aims at combining the benefits of
 Frameworks using interpreted languages (no compile/deploy/test delay)
 Type safety and checks of compiled languages
 Hot reload
 Framework listens for changes and compiles & deploys in background
 Change something, hit refresh in the browser, and you will see the effect
 Compiled, static-typed language is used
 Java or Scala
 Also templates and routes are type checked
Setup and project structure
17
Create a new play project
$ play new ewa-play-intro
_
_ __ | | __ _ _ _
| '_ | |/ _' | || |
| __/|_|____|__ /
|_| |__/
play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_25), http://www.playframework.com
The new application will be created in /home/whatever/ewa-play-intro
What is the application name? [ewa-play-intro]
Which template do you want to use for this new application?
1 - Create a simple Scala application
2 - Create a simple Java application
> 2
OK, application ewa-play-intro is created.
Have fun!
https://github.com/planger/ewa-play-intro/tree/a616c8b
Setup and project structure
18
The play console
Open the project folder (i.e., ewa-play-intro) in a console and start play
$ play
…
[ewa-play-intro] $ help play
These commands are available:
-----------------------------
classpath Display the project classpath.
clean Clean all generated files.
compile Compile the current application.
console Launch the interactive Scala console (use :quit to exit).
dependencies Display the dependencies summary.
dist Construct standalone application package.
exit Exit the console.
h2-browser Launch the H2 Web browser.
license Display licensing informations.
package Package your application as a JAR.
play-version Display the Play version.
publish Publish your application in a remote repository.
publish-local Publish your application in the local repository.
reload Reload the current application build file.
run <port> Run the current application in DEV mode.
test Run Junit tests and/or Specs from the command line
eclipse generate eclipse project file
idea generate Intellij IDEA project file
sh <command to run> execute a shell command
start <port> Start the current application in another JVM in PROD mode.
update Update application dependencies.
https://github.com/planger/ewa-play-intro/tree/a616c8b
Setup and project structure
19
Setup the IDE (Eclipse or IntelliJ) // you can also use any editor you like (compiling is done by Play)
[ewa-play-intro] $ eclipse
[info] About to create Eclipse project files for your project(s).
[info] Successfully created Eclipse project files for project(s):
[info] ewa-play-intro
In Eclipse
Import  Existing Projects into Workspace  Select
https://github.com/planger/ewa-play-intro/tree/a616c8b
Setup and project structure
20
Project structure
 app/
contains the application’s core in models, controllers, and views directories.
 conf/
contains all the application’s configuration files, especially the application.conf
file, the routes definition files, and the messages files used for internationalization.
 project/
contains the build scripts.
 public/
contains all the publicly available resources: JavaScript, stylesheets, and images.
 test/
contains all the application’s tests.
https://github.com/planger/ewa-play-intro/tree/a616c8b
Setup and project structure
21
Run the application
[ewa-play-intro] $ run
[info] Updating {file:/home/whatever/ewa-play-intro/}ewa-play-intro...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin[info]
Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
--- (Running the application from SBT, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
https://github.com/planger/ewa-play-intro/tree/a616c8b
Setup and project structure
22
Run the application
[ewa-play-intro] $ run
[info] Updating {file:/home/whatever/ewa-play-intro/}ewa-play-intro...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin[info]
Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
--- (Running the application from SBT, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
https://github.com/planger/ewa-play-intro/tree/a616c8b
Controllers and Routing
23
 Controller are static classes
 Located in app/controllers
 Inherit from an abstract Controller class
 Access to methods for obtaining request and sending back requests
 request().remoteAddress()  Access Request object and obtains address
 ok("Hello")  Result “x” with HTTP status 200
 Public static methods are also called “actions”
 Take arbitrary parameters
 Return Result object
public static Result sayHello() {
return ok("Hello " + request().remoteAddress());
}
public static Result sayHelloTo(String name) {
if (name.toLowerCase().matches("[a-z]")) {
String theName = name.toUpperCase();
return ok("Hello " + theName);
} else {
return badRequest("Provide a proper name!");
}
}
http://www.playframework.com/documentation/2.0/JavaActions
https://github.com/planger/ewa-play-intro/tree/ef1891c
Controllers and Routing
24
 Routing maps incoming requests to controller actions
 Syntax: <HTTPMethod> <URIPattern> <ControllerAction>
 HTTPMethod: GET, POST, DELETE, …
 Configured in conf/routes
 Routes are compiled and type safe too!
 URI patterns support
 Variable extraction (:name)
 Dynamic parts spanning several / (path*)
 Regular expressions ($name<[a-z]>)
 Controller actions support default values
# in conf/routes
GET /anonymoushello controllers.Application.sayHello()
GET /hello controllers.Application.sayHelloTo(name: String = "Stranger")
GET /hello/:name controllers.Application.sayHelloTo(name: String)
http://www.playframework.com/documentation/2.0/JavaRouting
https://github.com/planger/ewa-play-intro/tree/ef1891c
Templates with Scala
25
 Templates
 Located in app/views/
 Named *.scala.<format>
 Templates are compiled and are type-checked
 Example:
app/views/index.scala.html  class named views.html.index
To render a template from within a controller:
…
import views.html.*;
…
public static Result index() {
return ok(index.render("Your new application is ready."));
}
https://github.com/planger/ewa-play-intro/tree/ef1891c
Templates with Scala
26
 Templates
 They are basically Scala functions
 They may take parameters
 They may call other functions
 Static content mixed with Scala code using @
In main.scala.html
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen"
href="@routes.Assets.at("stylesheets/main.css")">
</head>
<body> @content </body>
</html>
http://www.playframework.com/documentation/2.0/JavaTemplates
https://github.com/planger/ewa-play-intro/tree/ef1891c
Templates with Scala
27
 Templates
 They are basically Scala functions
 They may take parameters
 They may call other functions
 Static content mixed with Scala code using @
In main.scala.html
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen"
href="@routes.Assets.at("stylesheets/main.css")">
</head>
<body> @content </body>
</html>
http://www.playframework.com/documentation/2.0/JavaTemplates
Reverse routing
GET /assets/*file controllers.Assets.at(path="/public", file)
https://github.com/planger/ewa-play-intro/tree/ef1891c
Templates with Scala
28
 Templates
 They are basically Scala functions
 They may take parameters
 They may call other functions
 Static content mixed with Scala code using @
In main.scala.html
@(title: String)(content: Html)
<!DOCTYPE html>
… @content …
In another.scala.html
@(message: String)
@main("This is the title of type String") {
<h1>This is the content of type Html</h1> <p>@message</p>
}
http://www.playframework.com/documentation/2.0/JavaTemplates
https://github.com/planger/ewa-play-intro/tree/ef1891c
Templates with Scala
29
 Example
app/controllers/Application.java
public static Result listCharacters(String text) {
List<Character> characters = new ArrayList<Character>();
for (char c : text.toLowerCase().toCharArray())
if (!characters.contains(c))
characters.add(c);
return ok(characterlist.render(text, characters));
}
app/views/characterlist.scala.html
@(text: String, characters: List[Character])
<!DOCTYPE html>
<html>
<head> <title>@text</title> </head>
<body>
<h1>Characters of @text</h1>
@charactersAsUnorderedList(characters)
</body>
</html>
@charactersAsUnorderedList(characters: List[Character]) = {
@if(!characters.isEmpty()) {
<ul>
@for(character <- characters) {
<li>@character</li>
}
</ul>
}
}
https://github.com/planger/ewa-play-intro/tree/c1b1594
Models and JPA
30
 Models
 Java or Scala classes that should be located in app/models/
 Represent the domain model (cf. MVC pattern)
 Basically, they are plain old Java objects
 Nothing special to be considered for working with them in Play
 Model objects are usually persisted in a database
 Play integrates Ebean1 by default (ORM persistence layer)
 But any other persistence layer may be used instead
 In this lecture, we use JPA2 & Hibernate3
 A brief excursus on object/relational mapping (ORM), JPA, and Hibernate
1 http://www.avaje.org/
2 http://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.html
3 http://hibernate.org/orm/
JPA: Java Persistence API
31
 API for managing the persistence of a Java domain model
 Object/relational mapping
 Java objects are persisted in a relational database
 Standardized under the Java Community Process Program
 Annotations for specifying persistent entities
 Object/relational mapping metadata
 API for storing, querying, and deleting objects
 Java Persistence Query Language (JPQL)
 Several JPA providers available
 Hibernate
 EclipseLink
 Apache OpenJPA
JPA: Persistent Entities
32
 Annotated Plain Old Java Objects
 Lightweight persistent domain object
 Persistent identity field
 Typically EJB style classes
 Public or protected no-arg constructor
 Getters and setters
 Support for
 Abstract classes and inheritance
 Relationships (OneToOne, OneToMany, ManyToMany)
 Each entity is typically represented by one table
 Each instance of an entity corresponds to a row in that table
 Entities may have both persistent and non-persistent state
 Persistent simple or complex typed data
 Non-persistent state (transient)
JPA: Persistent Entity
33
 Example
@Entity
@Access(AccessType.FIELD)
public class Employee {
@Id
private long id;
private String name;
@Transient
private Money salary;
@ManyToOne(fetch=FetchType.LAZY)
private Employee manager;
@Access(AccessType.PROPERTY)
private BigDecimal getSalary() {
return this.salary.toNumber();
}
private void setSalary(BigDecimal salary) {
this.salary = new Money(salary);
}
}
EMPLOYEE
ID NAME MANAGER_ID SALARY
Models and JPA
34
 Integrating Play with JPA and Hibernate
 Add dependencies (JPA and hibernate) to build.sbt
 Expose the datasource through JNDI in conf/application.conf
 Add persistence.xml to conf/META-INF
 Add dependency to Eclipse project (just for IDE support)
 Project Preferences  Java Build Path  Add External Library
 Browse to your Play local installation and select
play-2.2.1/repository/local/com.typesafe.play/play-java-jpa_2.10/2.2.1/jars/play-java-jpa_2.10.jar
 Reload dependencies in the Play console
$ play reload
http://www.playframework.com/documentation/2.2.x/JavaJPA
https://github.com/planger/ewa-play-intro/tree/15c5b4a
Models and JPA
35
 Using JPA in Play
 Model class with JPA annotations (javax.persistence.*)
@Entity
public class Pet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Gender gender;
… // getters and setters
 Controllers may access the JPA entity manager
 Actions have to be annotated with javax.persistence.Transactional
@Transactional
public static Result list() {
Collection<Pet> pets = getAllPets();
return ok(Json.toJson(pets));
}
private static Collection<Pet> getAllPets() {
EntityManager em = play.db.jpa.JPA.em();
String queryString = "SELECT p FROM Pet p";
TypedQuery<Pet> query = em.createQuery(queryString, Pet.class);
return (Collection<Pet>) query.getResultList();
}
http://www.playframework.com/documentation/2.2.x/JavaJPA
https://github.com/planger/ewa-play-intro/tree/d870e86
Forms and server-side validation
36
 Creating and processing dynamic forms
 Helpers to create HTML forms in templates
@helper.form(action = routes.Pets.createPetResponse) {
<select name="petId">
@for(pet <- pets) {
<option value="@pet.getId()">@pet.getName()</option>
}
</select>
<input type="text" id="petResponse" name="petResponse" />
<button type="submit" name="action" value="new">Respond</button>
}
 Helpers to handle HTTP form data in controllers
@Transactional
public static Result createPetResponse() {
DynamicForm form = Form.form().bindFromRequest();
String petId = form.data().get("petId");
String petResponse = form.data().get("petResponse");
Pet pet = getPetById(Long.valueOf(petId));
return ok(pet.getName() + " says " + petResponse);
}
http://www.playframework.com/documentation/2.0/ScalaFormHelpers
http://www.playframework.com/documentation/2.0/JavaForms
https://github.com/planger/ewa-play-intro/tree/75fb91d
Forms and server-side validation
37
 Creating and processing forms for model classes
 Helpers to create HTML forms in templates
<h1>New pet</h1>
@helper.form(action = routes.Pets.createPet) {
@helper.inputText(form("name"))
@helper.inputRadioGroup( form("gender"), options = Seq(("male"->"Male"),
("female"->"Female")))
<button type="submit" name="action" value="new">Save</button>
}
 Helpers to handle HTTP form data in controllers
@Transactional
public static Result createPet() {
Form<Pet> form = Form.form(Pet.class).bindFromRequest();
if (form.hasErrors()) {
return badRequest(petform.render(form));
} else {
Pet pet = form.get();
JPA.em().persist(pet);
return redirect(routes.Pets.list());
}
}
http://www.playframework.com/documentation/2.0/ScalaFormHelpers
http://www.playframework.com/documentation/2.0/JavaForms
https://github.com/planger/ewa-play-intro/tree/75fb91d
Forms and server-side validation
38
 Creating and processing forms for model classes
 Helpers to create HTML forms in templates
<h1>New pet</h1>
@helper.form(action = routes.Pets.createPet) {
@helper.inputText(form("name"))
@helper.inputRadioGroup( form("gender"), options = Seq(("male"->"Male"),
("female"->"Female")))
<button type="submit" name="action" value="new">Save</button>
}
 Helpers to handle HTTP form data in controllers
@Transactional
public static Result createPet() {
Form<Pet> form = Form.form(Pet.class).bindFromRequest();
if (form.hasErrors()) {
return badRequest(petform.render(form));
} else {
Pet pet = form.get();
JPA.em().persist(pet);
return redirect(routes.Pets.list());
}
}
http://www.playframework.com/documentation/2.0/ScalaFormHelpers
http://www.playframework.com/documentation/2.0/JavaForms
<form action="/pets" method="POST">
<dl class=" " id="name_field">
<dt><label for="name">name</label></dt>
<dd> <input type="text" id="name" name="name" value="" > </dd>
</dl>
<dl class=" " id="gender_field">
<dt><label for="gender">gender</label></dt>
<dd>
<span class="buttonset" id="gender">
<input type="radio" id="gender_male" name="gender" value="male" >
<label for="gender_male">Male</label>
<input type="radio" id="gender_female" name="gender" value="female" >
<label for="gender_female">Female</label>
</span>
</dd>
</dl>
<button type="submit" name="action" value="new">Save</button>
</form>
https://github.com/planger/ewa-play-intro/tree/75fb91d
Forms and server-side validation
39
 Server-side validation of model classes
 Several validation rules available in play.data.validation.Constraints
public class Pet {
…
@Constraints.Required
@Constraints.MinLength(4)
@Constraints.MaxLength(8)
private String name;
…
 Custom validation rules can be specified in a validate method
public List<ValidationError> validate() {
List<ValidationError> errors = null;
if (!Character.isUpperCase(name.charAt(0))) {
errors = new ArrayList<ValidationError>();
errors.add(new ValidationError("name", "Must start with upper case letter"));
}
return errors;
}
http://www.playframework.com/documentation/2.0/JavaForms
https://github.com/planger/ewa-play-intro/tree/9716829
Forms and server-side validation
40
 Displaying validation error messages
 Redirect back to form in case of an error
if (form.hasErrors()) {
return badRequest(petform.render(form));
}
 Validation errors are available in forms for custom messages
@for(error <- form("name").errors) {
<div id="error_msg_name" class="error" role="alert">
@error.message
</div>
}
 When using form helpers, error messages are rendered automatically
http://www.playframework.com/documentation/2.0/JavaForms
https://github.com/planger/ewa-play-intro/tree/9716829
Internationalization
41
 Specifying supported languages
 In conf/application.conf
application.langs=en,de
 Externalizing messages
 In conf/messages.<langcode>
pet.gender=Geschlecht
pet.response={0} sagt {1}.
 Using messages in templates
<div>@Messages("pet.gender")</div>
<div>@Messages("pet.response", pet.getName(), petResponse)</div>
 Using messages in Java using play.api.i18n.Messages
Messages.get("pet.gender")
Messages.get("pet.response", pet.getName(), petResponse)
http://www.playframework.com/documentation/2.0/JavaI18N
https://github.com/planger/ewa-play-intro/tree/d1bf60b
Authentication
42
 Authentication is realized using action composition
 Action composition enables to process actions in a chain
 Each action can modify the request before passing it on
 It may also decide to not pass the request (e.g., if not authenticated)
 Play already provides a built-in authenticator action
 Subclass play.mvc.Security.Authenticator
 getUsername(Http.Context ctx)
 Return null if not authenticated
 onUnauthorized(Http.Context ctx)
 Decide what to do if unauthorized
 Annotate entire controllers or single controller methods (actions)
@Security.Authenticated(<yoursubclass>.class)
public static Result createPet() {…}
https://www.playframework.com/documentation/2.2.x/JavaGuide4
Client state on the server
43
 Web server is stateless
 Sometimes we need to save client state
 E.g., to store whether a user is logged in already, shopping cart etc.
 Options to store a client state
 Session
 Stored in a cookie at the client
 Limited to 4 KB
 Hashed and encrypted (to prohibit manipulation)
 In a controller: session("key", "value"); session().remove("key");
 Cache
 Enables caching of objects and even entire HTTP responses
 Should not be used to store irreproducible data (well, it‘s a cache)
 In a controller: Cache.set("key", "value"); Cache.get("key");
 Datastore
 Larger irreproducible data should be stored in a database
https://www.playframework.com/documentation/2.2.x/JavaSessionFlash
https://www.playframework.com/documentation/2.2.x/JavaCache
Asynchronous results
44
 Useful for longer running controller actions
 E.g., when calling other web services or long-running database queries
 Avoids blocking a thread
 Play provides Scala‘s Promise API and asynchronous results
 A promise is a wrapper of a future value
 A Promise<T> will eventually yield the value T (or an error)
 For an asynchronous result, return a Promise<Result>
 Once the Promise<Result> yields the actual data
the result is sent to the client
public static Promise<Result> longRunningAction() {
Logger.info("Long running action entry");
WSRequestHolder duckduck = WS.url("https://www.duckduckgo.com");
Promise<Response> duckduckResponse = duckduck.get();
Promise<Result> result = duckduckResponse.map(toResult);
Logger.info("Long running action exit");
return result;
}
private static Function<Response, Result> toResult = new Function<Response, Result>() {
public Result apply(Response response) {
Logger.info("Inside the toResult function");
return ok(response.getBody()).as("text/html");
}
};
http://www.playframework.com/documentation/2.0/ScalaFormHelpers
http://www.playframework.com/documentation/2.0/JavaForms
https://github.com/planger/ewa-play-intro/tree/706f06e
References
45
 Code of these slides is available at github
 https://github.com/planger/ewa-play-intro
 Official documentation and tutorials
 http://www.playframework.com/documentation/2.2.x/JavaHome
 Parts of these slides are based on slides by Yevgeniy Brikman
 http://www.slideshare.net/brikis98/play-framework-async-io-with-java-and-scala
 https://www.youtube.com/watch?v=8z3h4Uv9YbE
 Another nice tutorial on Play
 https://www.youtube.com/watch?v=9_YYgl65FLs

More Related Content

What's hot

Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory UsageTomer Perry
 
Optimizing {Java} Application Performance on Kubernetes
Optimizing {Java} Application Performance on KubernetesOptimizing {Java} Application Performance on Kubernetes
Optimizing {Java} Application Performance on KubernetesDinakar Guniguntala
 
Modularização via BPL - Abordagem Prática para DataSnap & Front-end
Modularização via BPL - Abordagem Prática para DataSnap & Front-endModularização via BPL - Abordagem Prática para DataSnap & Front-end
Modularização via BPL - Abordagem Prática para DataSnap & Front-endMario Guedes
 
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_FinalGopi Krishnamurthy
 
Performance optimization for all flash based on aarch64 v2.0
Performance optimization for all flash based on aarch64 v2.0Performance optimization for all flash based on aarch64 v2.0
Performance optimization for all flash based on aarch64 v2.0Ceph Community
 
[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성NAVER D2
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slidesMohamed Farouk
 
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation Buffers
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation BuffersHBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation Buffers
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation BuffersCloudera, Inc.
 
Monitoring Flink with Prometheus
Monitoring Flink with PrometheusMonitoring Flink with Prometheus
Monitoring Flink with PrometheusMaximilian Bode
 
SFO15-TR9: PSCI, ACPI (and UEFI to boot)
SFO15-TR9: PSCI, ACPI (and UEFI to boot)SFO15-TR9: PSCI, ACPI (and UEFI to boot)
SFO15-TR9: PSCI, ACPI (and UEFI to boot)Linaro
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programmingRodolfo Finochietti
 
Scaling paypal workloads with oracle rac ss
Scaling paypal workloads with oracle rac ssScaling paypal workloads with oracle rac ss
Scaling paypal workloads with oracle rac ssAnil Nair
 
How to use Exachk effectively to manage Exadata environments OGBEmea
How to use Exachk effectively to manage Exadata environments OGBEmeaHow to use Exachk effectively to manage Exadata environments OGBEmea
How to use Exachk effectively to manage Exadata environments OGBEmeaSandesh Rao
 
Disaggregating Ceph using NVMeoF
Disaggregating Ceph using NVMeoFDisaggregating Ceph using NVMeoF
Disaggregating Ceph using NVMeoFShapeBlue
 
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...ScyllaDB
 
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...Spark Summit
 

What's hot (20)

Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory Usage
 
ACI DHCP Config Guide
ACI DHCP Config GuideACI DHCP Config Guide
ACI DHCP Config Guide
 
Optimizing {Java} Application Performance on Kubernetes
Optimizing {Java} Application Performance on KubernetesOptimizing {Java} Application Performance on Kubernetes
Optimizing {Java} Application Performance on Kubernetes
 
Modularização via BPL - Abordagem Prática para DataSnap & Front-end
Modularização via BPL - Abordagem Prática para DataSnap & Front-endModularização via BPL - Abordagem Prática para DataSnap & Front-end
Modularização via BPL - Abordagem Prática para DataSnap & Front-end
 
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final
03_03_Implementing_PCIe_ATS_in_ARM-based_SoCs_Final
 
Performance optimization for all flash based on aarch64 v2.0
Performance optimization for all flash based on aarch64 v2.0Performance optimization for all flash based on aarch64 v2.0
Performance optimization for all flash based on aarch64 v2.0
 
[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성[2D4]Python에서의 동시성_병렬성
[2D4]Python에서의 동시성_병렬성
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slides
 
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation Buffers
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation BuffersHBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation Buffers
HBase HUG Presentation: Avoiding Full GCs with MemStore-Local Allocation Buffers
 
Monitoring Flink with Prometheus
Monitoring Flink with PrometheusMonitoring Flink with Prometheus
Monitoring Flink with Prometheus
 
SFO15-TR9: PSCI, ACPI (and UEFI to boot)
SFO15-TR9: PSCI, ACPI (and UEFI to boot)SFO15-TR9: PSCI, ACPI (and UEFI to boot)
SFO15-TR9: PSCI, ACPI (and UEFI to boot)
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
 
Scaling paypal workloads with oracle rac ss
Scaling paypal workloads with oracle rac ssScaling paypal workloads with oracle rac ss
Scaling paypal workloads with oracle rac ss
 
How to use Exachk effectively to manage Exadata environments OGBEmea
How to use Exachk effectively to manage Exadata environments OGBEmeaHow to use Exachk effectively to manage Exadata environments OGBEmea
How to use Exachk effectively to manage Exadata environments OGBEmea
 
Disaggregating Ceph using NVMeoF
Disaggregating Ceph using NVMeoFDisaggregating Ceph using NVMeoF
Disaggregating Ceph using NVMeoF
 
Ceph on arm64 upload
Ceph on arm64   uploadCeph on arm64   upload
Ceph on arm64 upload
 
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
 
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
 
Qemu
QemuQemu
Qemu
 
Kamailio - Load Balancing Load Balancers
Kamailio - Load Balancing Load BalancersKamailio - Load Balancing Load Balancers
Kamailio - Load Balancing Load Balancers
 

Similar to Play Framework: The Basics

Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Ido Flatow
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introductionvstorm83
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet backdoor
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technologyMinal Maniar
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applicationshchen1
 
Play Support in Cloud Foundry
Play Support in Cloud FoundryPlay Support in Cloud Foundry
Play Support in Cloud Foundryrajdeep
 
Project report for final year project
Project report for final year projectProject report for final year project
Project report for final year projectsuneel singh
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET PresentationRasel Khan
 
The Play Framework at LinkedIn
The Play Framework at LinkedInThe Play Framework at LinkedIn
The Play Framework at LinkedInYevgeniy Brikman
 
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...jaxconf
 
The Top 10 Things Oracle UCM Users Need To Know About WebLogic
The Top 10 Things Oracle UCM Users Need To Know About WebLogicThe Top 10 Things Oracle UCM Users Need To Know About WebLogic
The Top 10 Things Oracle UCM Users Need To Know About WebLogicBrian Huff
 
Web development concepts using microsoft technologies
Web development concepts using microsoft technologiesWeb development concepts using microsoft technologies
Web development concepts using microsoft technologiesHosam Kamel
 
Play framework productivity formula
Play framework   productivity formula Play framework   productivity formula
Play framework productivity formula Sorin Chiprian
 

Similar to Play Framework: The Basics (20)

Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6
 
eXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework IntroductioneXo Platform SEA - Play Framework Introduction
eXo Platform SEA - Play Framework Introduction
 
Rahul Resume.doc
Rahul Resume.docRahul Resume.doc
Rahul Resume.doc
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
Java EE 7 introduction
Java EE 7  introductionJava EE 7  introduction
Java EE 7 introduction
 
Java ee introduction
Java ee introductionJava ee introduction
Java ee introduction
 
Play Support in Cloud Foundry
Play Support in Cloud FoundryPlay Support in Cloud Foundry
Play Support in Cloud Foundry
 
Project report for final year project
Project report for final year projectProject report for final year project
Project report for final year project
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET Presentation
 
Introduction to Node.JS
Introduction to Node.JSIntroduction to Node.JS
Introduction to Node.JS
 
The Play Framework at LinkedIn
The Play Framework at LinkedInThe Play Framework at LinkedIn
The Play Framework at LinkedIn
 
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
The Top 10 Things Oracle UCM Users Need To Know About WebLogic
The Top 10 Things Oracle UCM Users Need To Know About WebLogicThe Top 10 Things Oracle UCM Users Need To Know About WebLogic
The Top 10 Things Oracle UCM Users Need To Know About WebLogic
 
Web development concepts using microsoft technologies
Web development concepts using microsoft technologiesWeb development concepts using microsoft technologies
Web development concepts using microsoft technologies
 
Asp.net
Asp.netAsp.net
Asp.net
 
Play framework productivity formula
Play framework   productivity formula Play framework   productivity formula
Play framework productivity formula
 
Philly Tech Fest Iis
Philly Tech Fest IisPhilly Tech Fest Iis
Philly Tech Fest Iis
 

More from Philip Langer

Tailor made model comparison: How to customize EMF Compare for your modeling ...
Tailor made model comparison: How to customize EMF Compare for your modeling ...Tailor made model comparison: How to customize EMF Compare for your modeling ...
Tailor made model comparison: How to customize EMF Compare for your modeling ...Philip Langer
 
What every Eclipse developer should know about EMF
What every Eclipse developer should know about EMFWhat every Eclipse developer should know about EMF
What every Eclipse developer should know about EMFPhilip Langer
 
A Brief Introduction to Working with Git
A Brief Introduction to Working with GitA Brief Introduction to Working with Git
A Brief Introduction to Working with GitPhilip Langer
 
You need to extend your models? EMF Facet vs. EMF Profiles
You need to extend your models? EMF Facet vs. EMF ProfilesYou need to extend your models? EMF Facet vs. EMF Profiles
You need to extend your models? EMF Facet vs. EMF ProfilesPhilip Langer
 
Adaptable Model Versioning using Model Transformation By Demonstration
Adaptable Model Versioning using Model Transformation By DemonstrationAdaptable Model Versioning using Model Transformation By Demonstration
Adaptable Model Versioning using Model Transformation By DemonstrationPhilip Langer
 
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)From UML Profiles to EMF Profiles and Beyond (TOOLS'11)
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)Philip Langer
 
Colex: A Web-based Collaborative Conflict Lexicon
Colex: A Web-based Collaborative Conflict LexiconColex: A Web-based Collaborative Conflict Lexicon
Colex: A Web-based Collaborative Conflict LexiconPhilip Langer
 

More from Philip Langer (7)

Tailor made model comparison: How to customize EMF Compare for your modeling ...
Tailor made model comparison: How to customize EMF Compare for your modeling ...Tailor made model comparison: How to customize EMF Compare for your modeling ...
Tailor made model comparison: How to customize EMF Compare for your modeling ...
 
What every Eclipse developer should know about EMF
What every Eclipse developer should know about EMFWhat every Eclipse developer should know about EMF
What every Eclipse developer should know about EMF
 
A Brief Introduction to Working with Git
A Brief Introduction to Working with GitA Brief Introduction to Working with Git
A Brief Introduction to Working with Git
 
You need to extend your models? EMF Facet vs. EMF Profiles
You need to extend your models? EMF Facet vs. EMF ProfilesYou need to extend your models? EMF Facet vs. EMF Profiles
You need to extend your models? EMF Facet vs. EMF Profiles
 
Adaptable Model Versioning using Model Transformation By Demonstration
Adaptable Model Versioning using Model Transformation By DemonstrationAdaptable Model Versioning using Model Transformation By Demonstration
Adaptable Model Versioning using Model Transformation By Demonstration
 
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)From UML Profiles to EMF Profiles and Beyond (TOOLS'11)
From UML Profiles to EMF Profiles and Beyond (TOOLS'11)
 
Colex: A Web-based Collaborative Conflict Lexicon
Colex: A Web-based Collaborative Conflict LexiconColex: A Web-based Collaborative Conflict Lexicon
Colex: A Web-based Collaborative Conflict Lexicon
 

Recently uploaded

Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 

Recently uploaded (20)

Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 

Play Framework: The Basics

  • 1. Business Informatics Group Institute of Software Technology and Interactive Systems Vienna University of Technology Favoritenstraße 9-11/188-3, 1040 Vienna, Austria phone: +43 (1) 58801-18804 (secretary), fax: +43 (1) 58801-18896 office@big.tuwien.ac.at, www.big.tuwien.ac.at Play Framework: The Basics Building scalable web applications based on a non-blocking, stateless architecture and the Java Persistence API Philip Langer
  • 2. Outline 1. Motivation and overview 2. Setup and project structure 3. Controllers and routing 4. Templates with Scala 5. Models and JPA 6. Forms and server-side validation 7. Internationalization 8. Authentication 9. Client state on the server 10.Asynchronous results
  • 3. Motivation and Overview 3  Goals of the Play framework  Stateless web framework  As opposed to stateful  Based on an “evented” web server architecture  As opposed to threaded  Full-stack web framework  Including model persistence, template mechanism (view), controller, and testing  Aims at being  “developer-friendly” (e.g., hot reload mimicking interpreted languages)  fully compiled and type safe (even templates and routes)  Further nice features  RESTful by default  Integration of JSON  Integration of compilers for CoffeeScript, LESS, etc.  Support for long-living connections (WebSocket, Comet, …)  …
  • 4. Motivation and Overview 4  Stateful versus stateless web frameworks  Traditional web frameworks are stateful Server-side state is stored for each client in a session Web Server DB
  • 5. Motivation and Overview 5  Stateful versus stateless web frameworks  Traditional web frameworks are stateful Server-side state is stored for each client in a session Web Server DB
  • 6. Motivation and Overview 6  Stateful versus stateless web frameworks  Traditional web frameworks are stateful Server-side state is stored for each client in a session Load Balancer Web Server Web Server DB Web Server ?
  • 7. Motivation and Overview 7  Stateful versus stateless web frameworks  Traditional web frameworks are stateful Server-side state is stored for each client in a session Load Balancer Web Server Web Server DB Web Server
  • 8. DB Motivation and Overview 8  Stateful versus stateless web frameworks  Recently stateless web frameworks gained popularity  Web server instances do not store user state (“shared nothing”)  Stateful client & shared cache (memcached, MongoDB, …)  Allows to do horizontal scaling (adding/removing web server instances) Load Balancer Web Server Web Server Web Server DB
  • 9. DB Motivation and Overview 9  Threaded versus evented web servers  Thread pool maintains a number of threads  For each request, a thread is allocated until the request is completed Load Balancer Web Server Web Server Web Server Web Server Web Server Web Server Web Server Web Server
  • 10. DB Motivation and Overview 10  Threaded versus evented web servers  Thread pool maintains a number of threads  For each request, a thread is allocated until the request is completed  Thread is occupied but potentially idle due to long-running requests to other services Load Balancer Web Server Web Server Web Server Web Server Web Server Web Server Web Server Web Server public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { StockService s1 = new StockService(); PricingService s2 = new PricingService(); List<Stock> stocks = s1.requestStocks(); List<Price> prices = new ArrayList<long>(); for (Stock stock : stocks) { long price = s2.getPrice(stock.getId()); prices.add(price); } // ... renderResponse(response, prices); }
  • 11. DB Motivation and Overview 11  Threaded versus evented web servers  Thread pool maintains a number of threads  For each request, a thread is allocated until the request is completed  Thread is occupied but potentially idle due to long-running requests to other services Load Balancer Web Server Web Server Web Server Web Server Web Server Web Server Web Server Web Server When latency goes up here, threads are all occupied soon  new requests are queued.
  • 12. DB Motivation and Overview 12  Threaded versus evented web servers  Thread pool maintains a number of threads  For each request, a thread is allocated until the request is completed  Thread is occupied but potentially idle due to long-running requests to other services Load Balancer Web Server Web Server Web Server Web Server Web Server Web Server Web Server Web Server Latency goes up here too, threads get all occupied  new requests are queued too.
  • 13. DB Motivation and Overview 13  Threaded versus evented web servers  Thread pool maintains a number of threads  For each request, a thread is allocated until the request is completed  Thread is occupied but potentially idle due to long-running requests to other services Load Balancer Web Server Web Server Web Server Web Server Web Server Web Server Web Server Web Server same here… and here… and here… and here… and here…
  • 14. Motivation and Overview 14  Threaded versus evented web servers  Play is based on Netty An event-driven client-server framework  Play is based on Akka and the Promise/Future API Software library for building concurrent applications  For each CPU core, there is one thread/process  Enables asynchronous, non-blocking server requests/responses  Thread is occupied only, if there is something to do  No sensitivity to downstream latency
  • 15. Motivation and Overview 15  Full-stack framework  Model-view-controller architecture  Model and controller can be realized with Java or Scala  View is realized with a template syntax and Scala  Model persistence is based on Ebean by default  But any other persistence mechanism can be used too  We will use the Java Persistence API instead  Play integrates unit, integration, and system testing tools  JUnit  Dedicated APIs to call controllers and views  Selenium WebDriver and FluentLenium
  • 16. Motivation and Overview 16  Developer friendliness  Aims at combining the benefits of  Frameworks using interpreted languages (no compile/deploy/test delay)  Type safety and checks of compiled languages  Hot reload  Framework listens for changes and compiles & deploys in background  Change something, hit refresh in the browser, and you will see the effect  Compiled, static-typed language is used  Java or Scala  Also templates and routes are type checked
  • 17. Setup and project structure 17 Create a new play project $ play new ewa-play-intro _ _ __ | | __ _ _ _ | '_ | |/ _' | || | | __/|_|____|__ / |_| |__/ play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_25), http://www.playframework.com The new application will be created in /home/whatever/ewa-play-intro What is the application name? [ewa-play-intro] Which template do you want to use for this new application? 1 - Create a simple Scala application 2 - Create a simple Java application > 2 OK, application ewa-play-intro is created. Have fun! https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 18. Setup and project structure 18 The play console Open the project folder (i.e., ewa-play-intro) in a console and start play $ play … [ewa-play-intro] $ help play These commands are available: ----------------------------- classpath Display the project classpath. clean Clean all generated files. compile Compile the current application. console Launch the interactive Scala console (use :quit to exit). dependencies Display the dependencies summary. dist Construct standalone application package. exit Exit the console. h2-browser Launch the H2 Web browser. license Display licensing informations. package Package your application as a JAR. play-version Display the Play version. publish Publish your application in a remote repository. publish-local Publish your application in the local repository. reload Reload the current application build file. run <port> Run the current application in DEV mode. test Run Junit tests and/or Specs from the command line eclipse generate eclipse project file idea generate Intellij IDEA project file sh <command to run> execute a shell command start <port> Start the current application in another JVM in PROD mode. update Update application dependencies. https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 19. Setup and project structure 19 Setup the IDE (Eclipse or IntelliJ) // you can also use any editor you like (compiling is done by Play) [ewa-play-intro] $ eclipse [info] About to create Eclipse project files for your project(s). [info] Successfully created Eclipse project files for project(s): [info] ewa-play-intro In Eclipse Import  Existing Projects into Workspace  Select https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 20. Setup and project structure 20 Project structure  app/ contains the application’s core in models, controllers, and views directories.  conf/ contains all the application’s configuration files, especially the application.conf file, the routes definition files, and the messages files used for internationalization.  project/ contains the build scripts.  public/ contains all the publicly available resources: JavaScript, stylesheets, and images.  test/ contains all the application’s tests. https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 21. Setup and project structure 21 Run the application [ewa-play-intro] $ run [info] Updating {file:/home/whatever/ewa-play-intro/}ewa-play-intro... [info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin[info] Resolving org.fusesource.jansi#jansi;1.4 ... [info] Done updating. --- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 (Server started, use Ctrl+D to stop and go back to the console...) https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 22. Setup and project structure 22 Run the application [ewa-play-intro] $ run [info] Updating {file:/home/whatever/ewa-play-intro/}ewa-play-intro... [info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin[info] Resolving org.fusesource.jansi#jansi;1.4 ... [info] Done updating. --- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 (Server started, use Ctrl+D to stop and go back to the console...) https://github.com/planger/ewa-play-intro/tree/a616c8b
  • 23. Controllers and Routing 23  Controller are static classes  Located in app/controllers  Inherit from an abstract Controller class  Access to methods for obtaining request and sending back requests  request().remoteAddress()  Access Request object and obtains address  ok("Hello")  Result “x” with HTTP status 200  Public static methods are also called “actions”  Take arbitrary parameters  Return Result object public static Result sayHello() { return ok("Hello " + request().remoteAddress()); } public static Result sayHelloTo(String name) { if (name.toLowerCase().matches("[a-z]")) { String theName = name.toUpperCase(); return ok("Hello " + theName); } else { return badRequest("Provide a proper name!"); } } http://www.playframework.com/documentation/2.0/JavaActions https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 24. Controllers and Routing 24  Routing maps incoming requests to controller actions  Syntax: <HTTPMethod> <URIPattern> <ControllerAction>  HTTPMethod: GET, POST, DELETE, …  Configured in conf/routes  Routes are compiled and type safe too!  URI patterns support  Variable extraction (:name)  Dynamic parts spanning several / (path*)  Regular expressions ($name<[a-z]>)  Controller actions support default values # in conf/routes GET /anonymoushello controllers.Application.sayHello() GET /hello controllers.Application.sayHelloTo(name: String = "Stranger") GET /hello/:name controllers.Application.sayHelloTo(name: String) http://www.playframework.com/documentation/2.0/JavaRouting https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 25. Templates with Scala 25  Templates  Located in app/views/  Named *.scala.<format>  Templates are compiled and are type-checked  Example: app/views/index.scala.html  class named views.html.index To render a template from within a controller: … import views.html.*; … public static Result index() { return ok(index.render("Your new application is ready.")); } https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 26. Templates with Scala 26  Templates  They are basically Scala functions  They may take parameters  They may call other functions  Static content mixed with Scala code using @ In main.scala.html @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> </head> <body> @content </body> </html> http://www.playframework.com/documentation/2.0/JavaTemplates https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 27. Templates with Scala 27  Templates  They are basically Scala functions  They may take parameters  They may call other functions  Static content mixed with Scala code using @ In main.scala.html @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> </head> <body> @content </body> </html> http://www.playframework.com/documentation/2.0/JavaTemplates Reverse routing GET /assets/*file controllers.Assets.at(path="/public", file) https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 28. Templates with Scala 28  Templates  They are basically Scala functions  They may take parameters  They may call other functions  Static content mixed with Scala code using @ In main.scala.html @(title: String)(content: Html) <!DOCTYPE html> … @content … In another.scala.html @(message: String) @main("This is the title of type String") { <h1>This is the content of type Html</h1> <p>@message</p> } http://www.playframework.com/documentation/2.0/JavaTemplates https://github.com/planger/ewa-play-intro/tree/ef1891c
  • 29. Templates with Scala 29  Example app/controllers/Application.java public static Result listCharacters(String text) { List<Character> characters = new ArrayList<Character>(); for (char c : text.toLowerCase().toCharArray()) if (!characters.contains(c)) characters.add(c); return ok(characterlist.render(text, characters)); } app/views/characterlist.scala.html @(text: String, characters: List[Character]) <!DOCTYPE html> <html> <head> <title>@text</title> </head> <body> <h1>Characters of @text</h1> @charactersAsUnorderedList(characters) </body> </html> @charactersAsUnorderedList(characters: List[Character]) = { @if(!characters.isEmpty()) { <ul> @for(character <- characters) { <li>@character</li> } </ul> } } https://github.com/planger/ewa-play-intro/tree/c1b1594
  • 30. Models and JPA 30  Models  Java or Scala classes that should be located in app/models/  Represent the domain model (cf. MVC pattern)  Basically, they are plain old Java objects  Nothing special to be considered for working with them in Play  Model objects are usually persisted in a database  Play integrates Ebean1 by default (ORM persistence layer)  But any other persistence layer may be used instead  In this lecture, we use JPA2 & Hibernate3  A brief excursus on object/relational mapping (ORM), JPA, and Hibernate 1 http://www.avaje.org/ 2 http://www.oracle.com/technetwork/java/javaee/tech/persistence-jsp-140049.html 3 http://hibernate.org/orm/
  • 31. JPA: Java Persistence API 31  API for managing the persistence of a Java domain model  Object/relational mapping  Java objects are persisted in a relational database  Standardized under the Java Community Process Program  Annotations for specifying persistent entities  Object/relational mapping metadata  API for storing, querying, and deleting objects  Java Persistence Query Language (JPQL)  Several JPA providers available  Hibernate  EclipseLink  Apache OpenJPA
  • 32. JPA: Persistent Entities 32  Annotated Plain Old Java Objects  Lightweight persistent domain object  Persistent identity field  Typically EJB style classes  Public or protected no-arg constructor  Getters and setters  Support for  Abstract classes and inheritance  Relationships (OneToOne, OneToMany, ManyToMany)  Each entity is typically represented by one table  Each instance of an entity corresponds to a row in that table  Entities may have both persistent and non-persistent state  Persistent simple or complex typed data  Non-persistent state (transient)
  • 33. JPA: Persistent Entity 33  Example @Entity @Access(AccessType.FIELD) public class Employee { @Id private long id; private String name; @Transient private Money salary; @ManyToOne(fetch=FetchType.LAZY) private Employee manager; @Access(AccessType.PROPERTY) private BigDecimal getSalary() { return this.salary.toNumber(); } private void setSalary(BigDecimal salary) { this.salary = new Money(salary); } } EMPLOYEE ID NAME MANAGER_ID SALARY
  • 34. Models and JPA 34  Integrating Play with JPA and Hibernate  Add dependencies (JPA and hibernate) to build.sbt  Expose the datasource through JNDI in conf/application.conf  Add persistence.xml to conf/META-INF  Add dependency to Eclipse project (just for IDE support)  Project Preferences  Java Build Path  Add External Library  Browse to your Play local installation and select play-2.2.1/repository/local/com.typesafe.play/play-java-jpa_2.10/2.2.1/jars/play-java-jpa_2.10.jar  Reload dependencies in the Play console $ play reload http://www.playframework.com/documentation/2.2.x/JavaJPA https://github.com/planger/ewa-play-intro/tree/15c5b4a
  • 35. Models and JPA 35  Using JPA in Play  Model class with JPA annotations (javax.persistence.*) @Entity public class Pet { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Gender gender; … // getters and setters  Controllers may access the JPA entity manager  Actions have to be annotated with javax.persistence.Transactional @Transactional public static Result list() { Collection<Pet> pets = getAllPets(); return ok(Json.toJson(pets)); } private static Collection<Pet> getAllPets() { EntityManager em = play.db.jpa.JPA.em(); String queryString = "SELECT p FROM Pet p"; TypedQuery<Pet> query = em.createQuery(queryString, Pet.class); return (Collection<Pet>) query.getResultList(); } http://www.playframework.com/documentation/2.2.x/JavaJPA https://github.com/planger/ewa-play-intro/tree/d870e86
  • 36. Forms and server-side validation 36  Creating and processing dynamic forms  Helpers to create HTML forms in templates @helper.form(action = routes.Pets.createPetResponse) { <select name="petId"> @for(pet <- pets) { <option value="@pet.getId()">@pet.getName()</option> } </select> <input type="text" id="petResponse" name="petResponse" /> <button type="submit" name="action" value="new">Respond</button> }  Helpers to handle HTTP form data in controllers @Transactional public static Result createPetResponse() { DynamicForm form = Form.form().bindFromRequest(); String petId = form.data().get("petId"); String petResponse = form.data().get("petResponse"); Pet pet = getPetById(Long.valueOf(petId)); return ok(pet.getName() + " says " + petResponse); } http://www.playframework.com/documentation/2.0/ScalaFormHelpers http://www.playframework.com/documentation/2.0/JavaForms https://github.com/planger/ewa-play-intro/tree/75fb91d
  • 37. Forms and server-side validation 37  Creating and processing forms for model classes  Helpers to create HTML forms in templates <h1>New pet</h1> @helper.form(action = routes.Pets.createPet) { @helper.inputText(form("name")) @helper.inputRadioGroup( form("gender"), options = Seq(("male"->"Male"), ("female"->"Female"))) <button type="submit" name="action" value="new">Save</button> }  Helpers to handle HTTP form data in controllers @Transactional public static Result createPet() { Form<Pet> form = Form.form(Pet.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(petform.render(form)); } else { Pet pet = form.get(); JPA.em().persist(pet); return redirect(routes.Pets.list()); } } http://www.playframework.com/documentation/2.0/ScalaFormHelpers http://www.playframework.com/documentation/2.0/JavaForms https://github.com/planger/ewa-play-intro/tree/75fb91d
  • 38. Forms and server-side validation 38  Creating and processing forms for model classes  Helpers to create HTML forms in templates <h1>New pet</h1> @helper.form(action = routes.Pets.createPet) { @helper.inputText(form("name")) @helper.inputRadioGroup( form("gender"), options = Seq(("male"->"Male"), ("female"->"Female"))) <button type="submit" name="action" value="new">Save</button> }  Helpers to handle HTTP form data in controllers @Transactional public static Result createPet() { Form<Pet> form = Form.form(Pet.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(petform.render(form)); } else { Pet pet = form.get(); JPA.em().persist(pet); return redirect(routes.Pets.list()); } } http://www.playframework.com/documentation/2.0/ScalaFormHelpers http://www.playframework.com/documentation/2.0/JavaForms <form action="/pets" method="POST"> <dl class=" " id="name_field"> <dt><label for="name">name</label></dt> <dd> <input type="text" id="name" name="name" value="" > </dd> </dl> <dl class=" " id="gender_field"> <dt><label for="gender">gender</label></dt> <dd> <span class="buttonset" id="gender"> <input type="radio" id="gender_male" name="gender" value="male" > <label for="gender_male">Male</label> <input type="radio" id="gender_female" name="gender" value="female" > <label for="gender_female">Female</label> </span> </dd> </dl> <button type="submit" name="action" value="new">Save</button> </form> https://github.com/planger/ewa-play-intro/tree/75fb91d
  • 39. Forms and server-side validation 39  Server-side validation of model classes  Several validation rules available in play.data.validation.Constraints public class Pet { … @Constraints.Required @Constraints.MinLength(4) @Constraints.MaxLength(8) private String name; …  Custom validation rules can be specified in a validate method public List<ValidationError> validate() { List<ValidationError> errors = null; if (!Character.isUpperCase(name.charAt(0))) { errors = new ArrayList<ValidationError>(); errors.add(new ValidationError("name", "Must start with upper case letter")); } return errors; } http://www.playframework.com/documentation/2.0/JavaForms https://github.com/planger/ewa-play-intro/tree/9716829
  • 40. Forms and server-side validation 40  Displaying validation error messages  Redirect back to form in case of an error if (form.hasErrors()) { return badRequest(petform.render(form)); }  Validation errors are available in forms for custom messages @for(error <- form("name").errors) { <div id="error_msg_name" class="error" role="alert"> @error.message </div> }  When using form helpers, error messages are rendered automatically http://www.playframework.com/documentation/2.0/JavaForms https://github.com/planger/ewa-play-intro/tree/9716829
  • 41. Internationalization 41  Specifying supported languages  In conf/application.conf application.langs=en,de  Externalizing messages  In conf/messages.<langcode> pet.gender=Geschlecht pet.response={0} sagt {1}.  Using messages in templates <div>@Messages("pet.gender")</div> <div>@Messages("pet.response", pet.getName(), petResponse)</div>  Using messages in Java using play.api.i18n.Messages Messages.get("pet.gender") Messages.get("pet.response", pet.getName(), petResponse) http://www.playframework.com/documentation/2.0/JavaI18N https://github.com/planger/ewa-play-intro/tree/d1bf60b
  • 42. Authentication 42  Authentication is realized using action composition  Action composition enables to process actions in a chain  Each action can modify the request before passing it on  It may also decide to not pass the request (e.g., if not authenticated)  Play already provides a built-in authenticator action  Subclass play.mvc.Security.Authenticator  getUsername(Http.Context ctx)  Return null if not authenticated  onUnauthorized(Http.Context ctx)  Decide what to do if unauthorized  Annotate entire controllers or single controller methods (actions) @Security.Authenticated(<yoursubclass>.class) public static Result createPet() {…} https://www.playframework.com/documentation/2.2.x/JavaGuide4
  • 43. Client state on the server 43  Web server is stateless  Sometimes we need to save client state  E.g., to store whether a user is logged in already, shopping cart etc.  Options to store a client state  Session  Stored in a cookie at the client  Limited to 4 KB  Hashed and encrypted (to prohibit manipulation)  In a controller: session("key", "value"); session().remove("key");  Cache  Enables caching of objects and even entire HTTP responses  Should not be used to store irreproducible data (well, it‘s a cache)  In a controller: Cache.set("key", "value"); Cache.get("key");  Datastore  Larger irreproducible data should be stored in a database https://www.playframework.com/documentation/2.2.x/JavaSessionFlash https://www.playframework.com/documentation/2.2.x/JavaCache
  • 44. Asynchronous results 44  Useful for longer running controller actions  E.g., when calling other web services or long-running database queries  Avoids blocking a thread  Play provides Scala‘s Promise API and asynchronous results  A promise is a wrapper of a future value  A Promise<T> will eventually yield the value T (or an error)  For an asynchronous result, return a Promise<Result>  Once the Promise<Result> yields the actual data the result is sent to the client public static Promise<Result> longRunningAction() { Logger.info("Long running action entry"); WSRequestHolder duckduck = WS.url("https://www.duckduckgo.com"); Promise<Response> duckduckResponse = duckduck.get(); Promise<Result> result = duckduckResponse.map(toResult); Logger.info("Long running action exit"); return result; } private static Function<Response, Result> toResult = new Function<Response, Result>() { public Result apply(Response response) { Logger.info("Inside the toResult function"); return ok(response.getBody()).as("text/html"); } }; http://www.playframework.com/documentation/2.0/ScalaFormHelpers http://www.playframework.com/documentation/2.0/JavaForms https://github.com/planger/ewa-play-intro/tree/706f06e
  • 45. References 45  Code of these slides is available at github  https://github.com/planger/ewa-play-intro  Official documentation and tutorials  http://www.playframework.com/documentation/2.2.x/JavaHome  Parts of these slides are based on slides by Yevgeniy Brikman  http://www.slideshare.net/brikis98/play-framework-async-io-with-java-and-scala  https://www.youtube.com/watch?v=8z3h4Uv9YbE  Another nice tutorial on Play  https://www.youtube.com/watch?v=9_YYgl65FLs