Lightweight Programming Models
Describes the benefits of using dynamic languages along with the Java platform as a lightweight programming model and how to use Project Phobos.
Lightweight Programming with Dynamic Languages and the Java Platform
A growing number of developers prefer working with dynamic languages because they allow for more rapid development. The features that foster rapid development include:
Dynamic typing, which allows for more flexibility when building evolving systems, connecting different components, and extending existing software components.
No required compilation step, which means that developers can deploy applications without compiling them and can manipulate the code while it is running without having to redeploy the application.
Another advantage of dynamic languages in terms of developing with Ajax is that you can have the same language on the client and the server, thereby reducing the need to do conversions between the server-side code and the client-side code, such as converting Java object data to JavaScript values.
Although the characteristics of dynamic languages can provide advantages in some situations, they may come at a high cost in others. Some of the disadvantages associated with dynamic languages are:
Reduced execution speed resulting from additional runtime checks because of the lack of a compilation step.
Code that is more difficult to read compared to Java code.
When building more robust applications, developers prefer to catch as many errors as possible during compile time rather than at runtime. This is where a statically typed system language such as the Java programming language comes in.
You can use the Java programming language for those parts of the application that change less frequently, such as graphical user interface (GUI) components, and for those parts of the application that represent a performance bottleneck, such as methods that perform complex calculations or manipulate large amounts of data. And you can use scripting to connect these parts of an application. The ideal situation then would be working with a framework, such as Project Phobos, that allows you to use dynamic languages and the Java programming language where it makes sense to do so.
Project Phobos: A Lightweight Programming Model
Project Phobos offers a lightweight web application framework that runs on the Java platform but allows you to develop your entire application using a dynamic scripting language, such as JavaScript. As a result, you can take advantage of the many benefits that dynamic languages offer but still leverage the power of the Java platform. Phobos gives you what other scripting languages do not: access to the Java Platform, Enterprise Edition (Java EE) stack.
As the preceding section has pointed out, scripting languages and statically typed languages such as the Java programming language each have their own strengths. When you use Phobos to create web applications, you can use scripting and Java technology in ways that take advantage of their strengths. And because Phobos runs on the Java EE platform, you can call into components of the Java EE stack.
Phobos also simplifies development in Ajax. If you have JavaScript on the client, as you do with Ajax, and on the server, as you can with Phobos, you get all the benefits of having the same scripting language on both the client and the server. This also means that no translation is required between one language on the server and another on the client. In addition, Phobos includes a set of convenience libraries specifically for Ajax, such as the jMaki framework and the Dojo toolkit.
This tutorial first describes the Phobos architecture. It then uses a common calculator example to demonstrate the characteristics of a typical Phobos application. Next, it describes how to use Ajax in a Phobos application. Finally, it shows how to incorporate jMaki widgets into your Phobos application.
Phobos Architecture
Phobos consists of a set of scripting engines and a set of built-in scripting libraries. The scripting engines are compliant with JSR 223, Scripting for the Java Platform, and each one allows you to use a particular scripting language to develop your application. Currently, the version of Phobos included in the Sun Web Developer Pack supports the JavaScript engine.
The scripting libraries add convenience JavaScript functions for performing such common tasks as dispatching requests and rendering views, as well as more specialized ones such as integrating Ajax functionality into your application.
The application that you build with the Phobos framework adheres to a specific structure, which encourages building web applications according to the Model-View-Controller (MVC) design pattern. Every Phobos application consists of a set of controllers, a set of views, scripts for serving HTTP requests, and possibly other static content, such as style sheets and images.
Phobos also allows you to build your applications in standalone mode for prototyping purposes or as a web application. In fact, the NetBeans IDE 5.5.1 Phobos runtime module offers a lightweight HTTP server built on top of Grizzly that runs embedded inside the IDE. While developing your application, you can run it in the Phobos runtime and then export the application as a WAR file when you have completed it. Developing the Calculator Phobos Application Using the NetBeans IDE describes how to create a Phobos application that you can run in the Phobos runtime. Developing the bioFisheye Phobos Web Application Using the NetBeans IDE describes how to create a Phobos web application.
Once you have written your application, the flexible Phobos architecture allows you to run it on one of a variety of platforms. You can run a Phobos application on the open-source GlassFish server or in any compliant servlet container.
After you have deployed your application, it is live, meaning that you can make changes to the application while it is running and see the results instantaneously. There is no need to compile or redeploy the application. Let's take a closer look at what a Phobos application looks like.
Building a Simple Phobos Application
This tutorial includes a simple Calculator example built with the Phobos framework. Figure 5-1 shows a screenshot of the example running:
Figure 5-1 Calculator Example
This section uses the calculator example to explain the structure of a Phobos application and how to perform basic tasks while working with Phobos. See Building and Running the Calculator Application for instructions on how to build and run the example. See Developing the Calculator Phobos Application Using the NetBeans IDE for instructions on how to develop this example using NetBeans IDE 5.5.1.
Structure of a the Calculator Application
Every Phobos application includes an application directory, which in turn includes at least a controller, a script, and a view directory. Figure 5-2 shows the directory structure of the calculator example.
Figure 5-2 Directory Structure of the Calculator Example
As shown in Figure 1, the application directory contains the following subdirectories:
controller: In the controller directory, you put the scripting code that creates a controller object and defines the functions that are called in response to user actions.
script: In the script directory, you can put any extra scripting code you use in the application. In the case of the calculator, the script directory contains a script file that redirects requests to particular pages of the application.
view: In the view directory, you put the files that represent the different pages of the application.
More complicated applications might need additional directories, such as a static directory, in which you can put static files, such as HTML pages and CSS style sheets. Another directory that the NetBeans 5.5.1 IDE might create for you is the module directory. You can put application-specific scripts in this directory. The Overview of Phobos document describes the directory structure in more detail.
As the preceding figure shows, the Calculator application contains the following files:
calculator.js file, which instantiates a controller object and invokes the appropriate methods to perform the arithmetic operations and re-display the result.
index.js, which dispatches the first request for the application to the controller.
calculator.ejs, which is an embedded JavaScript file, an HTML file that contains JavaScript embedded in it. This file represents the page of the application.
The next section describes the role of each of these files during the processing of a request.
How the Calculator Application Works
Before delving into the different pieces of the calculator application, let's understand how it works. When the browser makes the first request for the page, the following happens:
The index.js file redirects the request to the /calculator/show URL. The calculator part of the URL is a controller, and show is a function of it. The show function is defined in calculator.js.
The Phobos runtime creates the Calculator controller and invokes the show function.
The show function sets the initial values of the operands, sets the selected arithmetic operation to add, and renders the calculator.ejs view.
The user enters two numbers, selects an operand, and clicks Compute.
When the user clicks Compute, the compute function of calculator.js is called by way of an HTTP POST to the /calculator/compute URL.
The compute function does the following:
Gets the values of the operands and the selected operation.
Performs the appropriate calculation, and saves the result into the user's HTTP session.
Redirects to the show function so that calculator.ejs is re-rendered.
The following sections describe the steps to create the calculator example.