Monday, June 6, 2011

Javascript "this" Keyword

In Javascript the "this" always refers the "owner" of the function being executed, or rather, to the object that a function is a method of. Below are a few different examples:

In this first example "this" refers to the global "window" object. If you do not reference a specific object then "this" refers to the global "window" object by default.

function doSomething()
{
  alert(this == window); //the result will be true
}
doSomething();

In this second example "this" refers to the HTML/DOM object that was retrieved using the getElementById method.

var element = document.getElementById('something');
function doSomething()
{
  alert(this == element); //the result will be true
}
element.onclick = doSomething;

In this third example "this" refers to the object that was instantiated using the "new" keyword. This is very similar to the previous example.

var object = new Object();
object.doSomething = function()
{
  alert(this == object); //the result will be true
}
object.doSomething();

Please be aware that you must pass "this" along as a parameter if you reference another function from an HTML event attribute such as onclick.

Click Here
function doSomething(reference)
{
  var element = document.getElementById('something');
  alert(reference == element); //the result will be true
  alert(this == element); //the result will be false
  alert(this == window); //the result will be true 
}

JSF 2.0 Overview

7/12/2014: I've not used JSF since I wrote this post years ago, but I've come to the conclusion that a complicated, abstracted lifecycle is a bad thing for a web framework. Therefore I would not recommend using JSF.

I have been learning JSF 2.0 (Java Server Faces 2.0) over the last few days. I never used the original JSF 1.0 implementation so I was slightly intimidated when I read mostly horrendous reviews regarding it. However, my understanding is that the second implementation is much improved and most of the problems have been corrected. I still have a few reservations about the complexity of the application lifecycle, but hopefully my concerns will diminish as I continue to learn the framework.

JSF is a Java web framework. Defined in greater detail, it is essentially a MVC framework, a GUI controls and handlers framework, and an AJAX framework. It competes with other frameworks like Servlet/JSP, Seam, Spring, Struts, Tapestry, Wicket, etc. (although in some cases two frameworks can be used together collaboratively i.e. JSF+Seam, JSF+Spring, etc). The original version of JSF attempted to collaborate with JSP but it quickly became clear that JSF potential was being burdened by JSP limitations. As a result, newer versions of JSF do not make use of JSP at all! Instead it uses a new technology called Facelets. That's a large paradigm shift for many of us who have been using JSP since we first learned Java web development. The current version of JSF is 2.0. JSF 1.0 had some noteable problems, but JSF 2.0 addresses many of them. JSF 2.0 also adds new features like AJAX support. With JSF 2.0 it is possible to add AJAX support with 100% Java code without writing any JavaScript code!

JSF is an officially supported framework. This may be perceived as an advantage compared to other 3rd party frameworks like Struts. There are many different flavors of JSF: Mojarra (basic Sun/Oracle implementation), MyFaces (basic Apache implementation), IceFaces (extended Sun/Oracle implementation), Tomahawk (extended Apache implementation), RichFaces (extended JBoss implementation), etc. Anyone is free to implement their own version of JSF so long as they adhere to the JSR specification. Following the JSR guidelines guarantees that all implementations will have the same basic functionality and features. However, implementors are allowed to go above and beyond the specification. Therefore, some implementations may have additional functionality and features that is vendor specific and not officially supported. This is both an advantage and a disadvantage. If you use an extended JSF implementation with fuller features and functionality you may eventually suffer from vendor lock-in.

To run JSF 2.0 the minimum requirements are Java 5.0 or greater, and an application server that supports Servlet spec 2.5 or greater. Unfortunately, this is a fairly stiff requirement since it eliminates the majority of older application servers. Supported servers include Tomcat 6+, Glassfish 3+, Weblogic 11+, JBoss 5.2+, etc. Newer versions of J2EE compliant servers like Glassfish, Weblogic, and JBoss will have automatic support for JSF. Since Tomcat is only a servlet container and is not J2EE compliant, you must manually install JSF (include 2 jar files). Although there are many different implementations of JSF created by many different vendors, they should run on any application server as long as the minimum requirements are met regardless of the vendor. So, for example, the RichFaces (JBoss) implementation should work ok on the Tomcat (Apache) application server.

Since JSF 2.0 is relatively new the documentation is still a little sparse. I recommend these tutorials for starting:

http://www.coreservlets.com/JSF-Tutorial/jsf2/

I found these tutorials to be excellent overall, except the AJAX tutorial was a little difficult to follow.

Sunday, June 5, 2011

Javascript Classes

The Javascript syntax is very loose, so it it possible to define a Javascript class using a variety of techniques. I prefer the following method because it is easiest to comprehend and most closely resembles the traditional syntax used in other programming languages. First, define a normal function to serve as the class definition (Javascript does not actually have a "class" keyword, instead it uses functions to simulate classes). Second, define properties and methods inside the function by using the "this" keyword. Last, instantiate an object from the function by using the "new" keyword. Pass in any arguments the same as you would with a normal constructor. Below is an example:

function Apple(type)
{
 this.type = type;
 this.color = 'red';

 this.getInfo = function()
 {
  return this.color + ' ' + this.type + ' apple';
 };
}

var apple = new Apple('macintosh');
apple.color = 'reddish';
alert(apple.getInfo());

Note: Some people prefer to use the "prototype" keyword to define Javascript classes because it is more memory efficient. You can read more about this alternative method by following the link at the bottom of this post.

I copied and paraphrased most of the information in this post from the following link:

http://www.phpied.com/3-ways-to-define-a-javascript-class/

Cloud Computing (and Google App Engine)

There's a lot of buzz about cloud computing lately, but often the term is used in a vague or ambiguous way (such as Microsoft's "Take it to the Cloud!" commercials). All the hype may leave you scratching your head and asking the question, "what exactly does cloud computing mean and why is it so popular?" The simplest definition of cloud computing refers to "the on-demand provision of computational resources (data, software) via a computer network, rather than from a local computer" (Wikipedia). Recently, Google has taken this definition to an extreme with the introduction of their Chromebooks; these are computers but all of the applications and data are stored on the web instead of on the harddrive.

So basically cloud computing is a movement to host more applications on the web instead of your computer. This definition alone is very loose, not particularly extraordinary, and does not fully explain the mystique of cloud computing. Actually, there are a few other qualifiers often attributed to cloud computing that contribute to its popularity especially regarding web development. A cloud service usually is elastic, is fully managed by the provider, and is sold on demand. I will attempt to explain these attributes in more detail in the following paragraphs.

A cloud service is elastic - "a user can have as much or as little of a service as they want at any given time". A cloud application is automatically scaled and load balanced as traffic increases; this is accomplished by automatically spinning up additional servers and distributing requests across these servers to meet demand. The obvious advantage here is that you do not have to manage these details yourself in the case that your application is growing larger and requires more resources (this leads into the next point).

A could service is fully managed by the provider - "the consumer needs nothing but a personal computer and Internet access". Cloud applications tend to run on a complex distributed network of virtualized machines that are both efficient and highly available. If one server goes down the burden will be shifted and another server will instantly replace it (guaranteeing nearly 100% up-time). This sophisticated architecture is fully supplied by the provider and mostly runs in the background; it does not require any physical resources from or direct administration by the user.

A cloud service is sold on demand - in other words you "pay for only for what you use" measured as combination of bandwidth usage, storage usage, number of requests, number of web service/api calls, etc. As a result, a user can take full advantage of the cloud services for a minimal investment. This can mean significant cost savings for businesses, especially small to medium sized businesses. Because of this cloud computing has become increasingly popular during this weak economy.

A few of the most prominent cloud providers are Amazon, Rackspace, Microsoft Azure, and Google. Although I believe Amazon is probably the most popular of these offerings, I have only experimented with Google App Engine, and it is my favorite so far because it supports a Java development environment and... it's free! (within limits, see below).

Google App Engine generously allows a free quota of 500MB of storage and 5 million page views per month. As a result I have used Google App Engine to host my Java applications for the past couple of years without paying a dime. If you want to host simple applications for personal use then I would highly recommend Google App Engine. However, there are some limitations associated with Google App Engine that can be frustrating. Therefore, if you want to host complex applications for commercial use then I would probably recommend a different provider such as Amazon.

I copied, quoted, and paraphrased much of the information in this post from the following link:

http://searchcloudcomputing.techtarget.com/definition/cloud-computing