Play-Framework-Cookbook.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Play Framework Cookbook Over 60 incredibly effective recipes to take you under the hood and leverage advanced concepts of the Play framework Alexander Reelsen BIRMINGHAM - MUMBAI Play Framework Cookbook Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: July 2011 Production Reference: 2290711 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-849515-52-8 www.packtpub.com Cover Image by Fillippo ([email protected]) Credits Author Project Coordinator Alexander Reelsen Joel Goveya Reviewers Proofreader Erik Bakker Aaron Nash Guillaume Bort Steve Chaloner Indexer Hemangini Bari Pascal Voitot Tejal Daruwale Acquisition Editor Eleanor Duffy Graphics Nilesh Mohite Development Editor Roger D’souza Production Coordinator Aparna Bhagat Technical Editor Kavita Iyer Cover Work Aparna Bhagat Copy Editor Neha Shetty Foreword Why Play is a small revolution in the Java world Looking at the past years of application development, as a developer you might have noticed a significant shift from desktop applications to web applications. The Web has evolved as the major platform for applications and is going to take over many facets—not only in development but also in everyday life, resulting in this shift accelerating. Who would have thought 10 years ago that current mobile phones are indeed only very strong ironed notebooks with a permanent Internet connection? The Internet provides a very direct connection between consumer and producer. For application developers this implies a very easy -to- use- and- handle platform. Looking around, many application frameworks have evolved in recent years in order to be very Internet-centric. These frameworks interpret the Web as an ubiquitous platform for providing not only ordinary web pages, as it was done 10 years ago. The web has become a data provider on top of one of the most proven protocols in industry, the HyperText Transfer Protocol (HTTP). The core concepts of the Internet being a decentralized highly available network with HTTP as a protocol on top of it are the inner core of a big part of today’s applications. Furthermore, another development took place in the last years. The browser became more and more a replacement of the operating system. Fully fledged web applications like Google Docs, which act and look like desktop applications, are becoming more popular. JavaScript engines like Google V8 or SpiderMonkey are getting insanely fast to deliver web browser performance not thought of several years ago. This means current web applications are now capable of delivering a real user experience similar to applications locally installed on your system. So many software engineers today are also web engineers as well. This poses a very big problem. As most of software engineering is based on abstraction, many tools, frameworks, and languages try to hide complexity from the engineer, which sounds good at first. No web engineer cares about the fragmentation of the IP packets which are sent throughout the whole world in milliseconds. By abstracting and layering your software to reduce the complexity per layer, you inadvertently might hide features of the underlying protocol your software is built upon. Many frameworks try to resemble the style of programming desktop applications, with the exception of application being in the Web. However, in order to make use of HTTP and its features you cannot easily hide them away in your application framework. This is especially a problem in the Java world. The defined standard is the servlet spec, which defines how web applications have to be accessible in a standard way. This implies the use of classes like HttpServletRequest, HttpServletResponse, HttpServlet, or HttpSession on which most of the available web frameworks are built upon. The servlet specification defines the abstraction of the HTTP protocol into Java applications. Though it is quite a good spec as HTTP carries quite some complexity around, it forces frameworks to obey certain conventions which never got challenged in the past. While many web frameworks like Django, Rails, or Symfony do not carry the burden of having to implement a big specification and do not need to fit into a big standardized ecosystem, most Java web frameworks have never questioned this. There are countless excellent web frameworks out there which implement the servlet specification, Grails, Tapestry, Google Web Toolkit, Spring Web MVC, and Wicket to name a few. However, there always was one gap: having a framework which allows quick deployment like Django or rails while still being completely Java based. This is what the Play framework finally delivers. This feature set does not sound too impressive, but it is. Being Java based implies two things: f Using the JVM and its ecosystem: This implies access to countless libraries, proven threading, and high performance. f Developer reusability: There are many Java developers who actually like this language. You can count me in as well. Have you ever tried to convince Java developers to use JavaScript as a backend language? Or PHP? Though Groovy and Scala are very nice languages, you do not want your developers to learn a new framework and a new language for your next project. And I do not talk about the hassle of IDE support for dynamic languages. Shortening development cycles is also an economic issue. As software engineers are quite expensive you do not want to pay them to wait for another “compile-deploy-restart” cycle. The Play framework solves this problem. All of the new generation web frameworks (Django in Python, Rails on Ruby, expressjs on top of nodejs in JavaScript) impose their own style of architecture, where HTTP is a first class citizen. In Java, HTTP is only another protocol that a Java application has to run on. So the Play framework is a pure Java-based solution, which brings a real HTTP focused framework with lots of helpers to speed up development resulting in shorter iterations and faster deployments. This book should help you to get the most out of the Play framework and to be as fast as any other developer on any platform when creating web applications. Prerequisites I made several assumptions about the persons reading this book. One of the first assumptions is that you already have used Play a little bit. This does not mean that you have deployed a 20 node cluster and are running a shop on top of it. It means that you downloaded the framework, took a brief look at the documentation, and ran through a few of the examples. While reading the documentation you will also take a first look at the source, which is surprisingly short. I will try to repeat introductory stuff only when it is necessary and I will try to keep new things as short as possible, as this is a cookbook and should come with handy solutions in more complex situations. What is missing: A Scala chapter No book is perfect. Neither is this. Many people would be eager to read a chapter about integration of Play and Scala. When I started writing this book, my Scala knowledge was far from competitive (and still is in many areas). Furthermore I currently do not think about using Scala in a production web application together with Play. This will change with growing maturity of the integration of these two technologies. Alexander Reelsen About the Author Alexander Reelsen is a software engineer living in Munich, Germany, where he has been working on different software systems, for example, a touristic booking engine, a campaign management and messaging platform, and a b2b ecommerce portal. He has been using the Play framework since 2009 and was immediately astonished by the sheer simplicity of this framework, while still being pure Java. His other interests includes scaling shared-nothing web architectures and NoSQL databases. Being a system engineer most of the time, when he started playing around with Linux at the age of 14, Alexander got to know software engineering during studies and decided that web applications are more interesting than system administration. If not hacking in front of his notebook, he enjoys playing a good game of basketball or streetball. Sometimes he even tweets at http://twitter.com/spinscale and can be reached anytime at [email protected]. If I do not thank my girlfriend for letting me spend more time with the laptop than with her while writing this book, I fear unknown consequences. So, thanks Christine! Uncountable appreciation goes out to my parents for letting me spent days and (possibly not knowing) nights in front of the PC, and to my brother Stefan, who introduced me into the world of IT - which worked pretty well until now. Thanks for the inspiration, fun, and fellowship to all my current and former colleagues, mainly of course to the developers. They always open up views and opinions to make developing enjoyable. Many thanks go out to the Play framework developers and especially Guillaume, but also to the other core developers.