Saturday, March 24, 2007

Starting a New Project

I am going to start a new project with Phobos so I can become familiar with how to write a web site and convert existing web sites to the Java platform. The first thing I did was read the Phobos overview located at the following URL https://phobos.dev.java.net/overview.html. If you come from a Java background the architecture is probably familiar to you. If you have been programming purely with html pages and script engines, the architecture may be a little confusing. It took me a few times through the documentation to get a few basic concepts.
So let's start with the first thing every web site needs and that is a layout. We will use the default jmaki layout for our sample app. If you start a new project in Netbeans and choose the default Phobos app you will be prompted to choose a layout. Choose any layout and click finish.
You will notice that I have an extra directory which was not created by the wizard called dynamic. Add this if you are planning to do this example. The Phobos team will be adding this soon. The dynamic folder along with the ejsp file extension is a new feature that is not in the documentation yet. It is a special folder which is accessed by the system whenever a file with the ejsp extension is requested. You can put any type of file in there and use it to script just like you would ASP or PHP. This is important to note as this is not the standard procedure for Java type applications.
Before we go into detail about the mechanics, lets follow a classic ASP example of how we would write a basic page with a layout. The default home page for the application is /script/index.js. Here is the code for it currently:

/* This script is called when the client tries to access the "/" URL.
* It simply redirects to a URL that causes the main application page
* to be displayed.
*/
// default index is to activate the main controller or page:
library.httpserver.sendRedirect(library.httpserver.makeUrl("default.ejsp"));

We are simply using the two library functions to redirect the browser to our home page. Because we used the ejsp extension, Phobos knew to look in the dynamic directory for the file. We did not have to give it a full path.
The code for our home page looks like this:

<% library.view.include("/application/dynamic/header.ejs"); %>

Hello from embedded Javascript running on a JDK


<% library.view.include("/application/dynamic/footer.ejs"); %>

The
library.view.include function includes content from another file just like and include directive in ASP or PHP. So we included our header and footer and put our main content in between. We did have to provide the full path to the files because the extension is ejs. We could have just used the file name if our header and footer were located in the view directory. Phobos would have found them for us in that case.
When creating hyperlinks in our main content, we can link directly to file names in the dynamic folder and include our layout like we did above. This is typically how scripting has worked on the web and what scripters are used to.

Now we will look at how Java typically has done the same thing so we can understand the whole Phobos framework. You will notice in the directory layout that we have a controller folder and a view folder. In Java, you would use what they call a Model View Controller framework
. If you are trying to visualize with the layout page in mind compared to the example above, what you need to understand is that we essentially link to the same controller ("file") with our hyperlinks and swap out the center content section of the page. This has its advantages as you will see as we progress, and it is still very flexible.

So with MVC we always have at least two files. We have the controller and the view. The model is the data shared between the two. In our controller we would put all of our script code and then paint the view. The view can have presentation code in it as well and adjust itself based on the data in the model. Here is the modified code for the index.js when we are going to use the controller:

/* This script is called when the client tries to access the "/" URL.
* It simply redirects to a URL that causes the main application page
* to be displayed.
*/
// default index is to activate the main controller or page:
library.httpserver.sendRedirect(library.httpserver.makeUrl("/main/show"));

Here we are sending a redirect, and Phobos knows where the controllers are so we do not have to give it the path. We are calling the show method of the controller.

Here is the code for the main controller:

/*
* Main controller for the application.
*
* This file was generated, please edit it as needed.
*/
// define a controller module called "main"
library.common.define(controller, "main", function() {
// controller class (constructor)
function Main() {
// insert instance initialization code here
}
// "show" action method
Main.prototype.show = function() {
/*
Use the model global variable to pass information to the view.
Alternatively, a view can access the controller instance by
using the expression:
invocation.controller
*/
model = {};
// render the /application/view/main.ejs view
//library.view.render("main.ejs");
library.view.render({view: "mainContent.ejs",
layout: "main.ejs"});
}
// export the controller class
this.Main = Main;
});

Consider this commented line:

//library.view.render("main.ejs");
This line would have rendered the view called main.ejs. However, we can have a layout file and a different file with the content for the main section. These commands will render both files:
library.view.render({view: "mainContent.ejs",
layout: "main.ejs"});
The
mainContent.ejs is the center section of the page and the main.ejs is the layout.

The main views are just made up of html except that in the layout where the center content is placed, you place a function call with the following line:

<% library.view.layoutContent(); %>

I hope this has shown that Phobos can act like a traditional script engine and is flexible. I have attempted also to explain the semantics behind the MVC pattern which is a much powerful and maintainable when writing large applications.

Wednesday, March 21, 2007

Why Phobos?

Why choose Phobos for a scripting environment? What is exciting about Phobos is that it uses Javascript for the server side scripting language. There are plans to plug in other languages as well, but Javascript is supported right out of the box. Javascript is becoming the language that spans environments. Javascript is built into the latest Java Virtual machine and can be used in the Java environment. Javascript is used in every web browser. Javascript is also used in the Mozilla framework which is the foundation for Firefox and the Thunderbird email client. Javascript is easy to learn, and there are millions of examples on the web for doing just about anything.

Phobos gives someone who does scripting, a native scripting environment. Java is powerful, but more complex and you have to deploy a web app to your server. With Phobos, you can deploy a shell web app and then use any html/programming editor to write files and scripts. Phobos will compile and run them when they are requested just like ASP. This is another strength and departure from ASP.NET where you are forced into using a version of Visual Studio.

NetBeans is the main editor for working on a Phobos project. It is a full IDE like Visual Studio and has Phobos javascript debugging as well as editors for html and css. However, if you would rather build something in your favorite html editor, or design software, that will work to.

Some people may say that you could not build large complex websites with classic ASP, but there were plenty of them out there and everyone knows that if you needed complex processing, you used COM components with ASP as the glue. With Phobos you can do the same thing. You can use Phobos as the glue, and you can use any Java objects you would like to, and access them from the scripting environment. That means you can grab an open source Java component and use its functionality without really having to learn Java. The truth is, a large portion of web sites are small and developed by part time developers. In the last five years, Microsoft has discontinued development of VB6, ASP, COM and now Visual Foxpro. Java, PHP, Perl, and many other languages are still going strong. Give open source a try. It is getting easier.

Phobos is still developing and you have direct access to the Sun engineers working on it. Join the mailing list users@phobos.dev.java.net, download Phobos and try porting one of your applications to it.