Understanding the life cycle of a Play request! expression

I'm new to Play! web framework, and in order to understand how it works, as well as how it compares with other web frames, I would like to track on Play! source code, request life cycle from start to finish. I will use the Scala Play! Implementation.

Since most of my experiences were related to PHP frameworks, I’m used to starting with the index.php file in the root directory on the network and reading through any included configuration / boot scripts, handling dependency injection, query routing, dispatching actions, and finally viewing /answer.

I could not identify a similar entry point for Scala / Play! application, and I would really like to get a push in the right direction. Going through the request life cycle will of course be very generous, but I really need to show the entry point.

+7
source share
3 answers

By default, the Play Framework uses an embedded HTTP server (based on Netty ). Therefore, the closest analogy to PHP will be that Play is both Apache and PHP.

PHP uses the outdated "CGI-like" paradigm: to serve one HTTP request, your program starts and after the service request is completed it stops. In CGI, to serve an HTTP request, the web server launches an external program - your script - and returns its output. Old versions of PHP were developed only for CGI, in later versions - other ways of interacting with the server, since CGI is very slow, but the basic principle remains the same.

Most web application technologies take a different approach: your web application starts once and then remains running, so one running instance of the web application continues to serve requests (and can handle multiple requests in parallel). It does not die after submitting a single request, as in PHP. This allows you to consume much less resources needed to run the application each time, and only a little more difficult to work, because most of the processing of requests hidden inside the framework and your application should only expose the controller methods that are called when the request is received and the response is returned.

It also provides great flexibility, for example, background processing can be started directly in a web application, without the need for external server processes. The game has Akka library, which is very convenient for this.

As more and more web applications use the Ajax and REST approach, instead of showing heavyweight web pages each time, this becomes more important. And it’s almost impossible to create a real-time instant messaging server with PHP, which will have good performance regardless of the requesting technology (polling, long polling, iframe with multipart).

But when compared to the PHP MVC frameworks, from the point of view of the developer who creates the views, models and controllers, Play is very similar. Both in the PHP MVC frameworks and in the Play Framework, the method or function of the controller is called, and this method should return a response, views are usually templates, and models are usually ORM bindings to a relational database.

+3
source

I think this is the file you have in mind:

https://github.com/playframework/playframework/blob/master/framework/src/play-netty-server/src/main/scala/play/core/server/NettyServer.scala

Playback is a Java application that starts listening on this port. Listening is done using the Netty library, which understands the different types of network protocols (most importantly, HTTP). Once Netty knows what is going on, it will control the playback structure.

The Play Framework will then use the Global File in combination with Routes to determine which action to invoke.

+2
source

Playback is more like a rest system (read http://en.wikipedia.org/wiki/Representational_state_transfer ), rather than typical template-based frameworks like jsp jsf etc. with the query life cycle, a concept, although it also has support for templates. The main idea is that the interaction with the server is based on pure data, such as json, and most of the code for updating the dom structure is written in javascript and runs only on the client, which is actually more flexible and much simpler and more efficient.

In the game, you simply create methods for sending data to the browser, defining a method in your scala class and matching it in the routes file. Just like in a typical web development process, you also place html files in a share folder (or create a template), which usually makes an ajax call for this method when executed in a browser.

+1
source

All Articles