Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using HTTP cookies


For this recipe, we will explore how Play applications can manipulate HTTP cookies. We will use the curl tool to validate our changes to the HTTP response headers containing the new cookie we added to the response.

How to do it…

For Java, we need to take the following steps:

  1. Run the foo_java application with Hot-Reloading enabled.

  2. Modify foo_java/app/controllers/Application.scala by adding the following action:

      public static Result modifyCookies() {
            response().setCookie("source", "tw", (60*60));
            return ok("Cookie Modification Example");
         }
  3. Add a new route entry for the newly-added action in foo_java/conf/routes:

      GET   /cookie_example    controllers.Application.modifyCookies
  4. Request our new route and examine the response headers to confirm our modifications to the HTTP response header:

    $ curl -v http://localhost:9000/cookie_example
    * Hostname was NOT found in DNS cache
    *   Trying ::1...
    * Connected to localhost (::1) port 9000 (#0)
    > GET /cookie_example...