Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Use Continuations to Develop Complex Web Applications

By Abhijit Belapurkar
2005-04-22


The Case for Continuations

A continuations-based Web application neatly sidesteps the above-mentioned problems associated with Web application development. In contrast to MVC-based Web applications, a continuations-based application is written as a single monolithic program. Anytime the program requires input from the user, a Web page containing the relevant forms is sent back to the user's browser, and a continuation representing the remaining part of the application logic is generated and put away. (I will soon explain your options for "putting away" the continuation.) Because it is important to be able to restart the remaining part of the application logic upon receiving the user's response, a unique id is also generated to serve as the key to search for this particular continuation in the continuations repository. This id is also sent down, along with the page displayed to the user in an appropriate manner such that the form submission will cause the id to be sent back in the response.

It's important to note that the continuations-based program behaves no differently from any other stand-alone program that uses blocking reads and writes. All the usual programming constructs can be used, including branching on if conditions, for and while looping, and more. The only difference is that in an MVC model, the application code using these constructs would be scattered across different modules and pages, but in the continuations-based program, the entire logic is contained within one program.

When the required response arrives from the user, the server, which is providing the continuations infrastructure for the deployed Web application, retrieves the continuation id from the response -- along with the regular submitted data -- and retrieves the continuation from the repository using this id. The continuation is then resumed (that is, the application logic starts executing starting from the line of code immediately following the line in which the continuation was created). Typically, the first few lines will extract the rest of the data submitted by the user, implying that the request object must be made available by the server within the program, and the business logic will continue with this received data.

Upon resuming a continuation, the code that starts executing may require more input data from the user, which will necessitate further interaction. This is handled by creating a second continuation, saving it in the continuations repository, and sending the form in which the appropriate continuation id is embedded to the user. The generated outer and inner continuations can be seen to form a tree structure with the former being a parent node and the latter a child node. Similarly, if the business logic has conditional code (different actions based on the outcome of an if clause) and both branches led to the formation of continuations corresponding to possibly different data-gathering pages being sent back to the browser, both these continuations will be the children of the outer continuation and siblings of each other. In this way, the complete application code can be seen to correspond to a tree of continuations -- a forest of continuations.

Remember blocking I/O?
The approach described in this article uses continuations in a typical read-write program (or the equivalents of reads and writes in the language being used). To summarize: A call to a read causes the program to block and resume only after the user has provided an input. The provided input is used to continue the application logic. Any intermediate output is displayed to the user via a non-blocking write call. The next time the program requires user input, it makes another call to read and blocks again. This sequence continues until the program terminates.

Tutorial Pages:
» A Programming Paradigm to Simplify MVC for the Web
» What is a Continuation, Anyway?
» Problems in Conventional Web development
» The Case for Continuations
» User-Centered Navigation
» You Make the Rules!
» The Continuations Repository
» An Example Application
» Web Continuations in Apache Cocoon
» The Application Sitemap
» The Application Logic
» Understanding the Application Logic
» Resuming the Continuation
» JavaScript vs. the Java language
» Continuations in Java code
» Pros and Cons of Continuations
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources