Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Java for CFers Part 1 of 3
Java for CFers Part 1 of 3

Java is a reality. But for many CFers the buzz and hype surrounding Java is cause for much concern. For those of us who love ColdFusion because of its simplicity, Java can indeed seem intimidating. Much of that concern is legitimate. The fact of the matter is, there's no way you'll learn Java as quickly as you learned ColdFusion, and you'll definitely not be as productive as quickly.

Having said that, Java does have lots to offer. It's fast, it scales well, it runs on a wide range of platforms, it supports all sorts of services and extensions. There's a seemingly endless supply of Java-related resources out there and - perhaps most important - Java is respected by the development community (considerably more so than ColdFusion is). And, as I've mentioned in previous columns, a Java/CF convergence of sorts is on the way (not that you'll have to learn Java, but you'll be able to leverage it if you so desire).

In this column I'd like to start exploring Java (and JSP - JavaServer Pages - specifically) from a ColdFusion developer's perspective - and the best place to start is with some simple comparisons.

Note: If you'd like to tinker with JSP, and I strongly recommend you doing so, download an evaluation version of JRun from the Allaire Web site. JRun is a top-notch J2EE server with superb JSP support, and it also integrates nicely with ColdFusion (as you'll see in a future issue).

Introducing JSP
Java has lots to offer, but it's a pain to learn. What to do? The answer is JSP. JSP is essentially a tag-based scripting-style interface to Java development. With JSP you can write Java-based applications much like you'd write code in ColdFusion (or ASP and Perl, for that matter).

As I explained in a previous column ("ColdFusion and Java - A Match Made in E-Heaven," CFDJ, Vol. 2, issue 4), Java is both a platform and a language, and the two are often confused. The Java platform is used for more than Web applications, although Java-based Web apps are what's of interest to us now. One way to create these apps is to use servlet technology - a servlet is a special form of Java application designed specifically for Web-based applications. In fact, you can think of a servlet as a compiled script (imagine compiling your CFM pages into binary code). Servlets provide simplified access to everything from GET and POST data to cookies and a whole lot more. And servlets are written in Java.

Or at least they were until JSP came along. Using JSP you can create servlets using tags and scripting. The JSP pages are then compiled and eventually become servlets, all without your doing anything special to make that happen. JSP is actually very similar to ColdFusion in that it processes scripts and generates output. But unlike ColdFusion, JSP generates compiled code that can be executed far quicker on subsequent requests. In other words, JSP provides you with a path to Java without your actually having to learn much Java at all.

Some Basics
Let's start with some syntax basics. CFML developers use tags, <CFSET>, <CFOUTPUT>, and so on. JSP developers also use tags, all of which start with <% and end with %>.

<%! %> is used to declare or define variables or functions. You'd use <%! %> wherever you'd have used a <CFSET> tag. And like <CFSET>, <!% %> is not used to display any output.

<% %> is used to delimit a script block (perhaps in Java or Java-Script). You can think of <% %> as being kind of like <CFSCRIPT> and </CFSCRIPT>.

<%= %> is used to evaluate expressions, much like <CFOUTPUT>, and for the most part you'd use it wherever you'd use <CFOUTPUT> tags.

<%@ %> has no real ColdFusion parallel (although it does the job of lots of different ColdFusion tags and options); it's used to specify directives - page-wide properties that affect the generated servlet. For example, you'd use a directive to set the output MIME type (equivalent to <CFCONTENT>) and to turn on session-state management (equivalent to <CFAPPLICATION>).

Note: JRun Studio is the editor of choice for JSP development. But if you have ColdFusion Studio already, you'll be pleased to know that it comes with basic JSP support and color coding built right in.

Using Variables
Let's start with something simple - variable assignment. The following ColdFusion code creates a variable and assigns a value to it:

<CFSET name="Ben">

This is the equivalent JSP code:

<%! String name="Ben"; %>

The big difference here is that the JSP code also defines the variable type. Actually, the type could have been left out, but most JSP developers like to state the types explicitly to avoid possible confusion or ambiguity. Another difference is that a single tag set can be used to define multiple variables, like this:

<%! String fname="Ben", String lname="Forta"; %>

Now that you know how to create variables, let's look at what it takes to use them. Here's a simple ColdFusion code snippet:

<CFOUTPUT>Hello #name#</CFOUTPUT>

And here's the JSP equivalent:

Hello <%=name%>

In truth, the examples aren't the same. The line of JSP code I showed you is more like this (with the <CFOUTPUT> tags just around the variable, not including the word Hello):

Hello <CFOUTPUT>#name#</CFOUTPUT>

Unlike ColdFusion, which assumes that all text within an expression is literal text unless explicitly flagged as not (by using pound signs), JSP assumes that all text within an expression is an expression to be processed. If you want to include literal text within your expression, you'll need to let the JSP processor know that it is literal text by enclosing it within quotes, like this:

<%="Hello "+name%>

Conditional Processing
Conditional processing in JSP is also quite similar to CFML. Here's a typical CFML <CFIF> statement - it evaluates an expression and displays one of two possible outputs depending on whether or not the expression is zero:

<CFIF cart_contents GTE 1> <CFOUTPUT>#cart_contents# items in cart</CFOUTPUT> <CFLESE> Your cart is empty </CFIF>

And here's the JSP equivalent:

<% if (cart_contents >= 1) { %> <%=cart_contents%> items in cart <% } else { %> Your cart is empty <% } %>

This JSP code contains three script blocks (each enclosed within <% and %> tags). One script block could have been used, but then the code to generate the output would have been a little more complex.

Using Loops
Loops are no more complex than if statements. Here's a CFML loop - it loops from 1 to 10, displaying the current loop count as it goes:

<CFLOOP FROM="1" TO="10" INDEX="i">
<CFOUTPUT>#i#</CFOUTPUT>
<CFLOOP>

Here's the JSP equivalent:

<% for(int i=1; i<10; i++) { %>
<%=i%><BR>
<% } %>

The JSP for() loop takes three parameters: the first is the start value (here a variable is defined and assigned the start value in one step); the second is the condition that must be true for the loop to continue (here the loop will continue as long as i is less than 10); the third is an action to be performed on each iteration (here i++ is used to increment i on each loop; to loop backwards, i could have been used). This same for() loop can be used to check for any condition (like <CFLOOP CONDITION=""> in CFML).

Using Includes
ColdFusion has supported in-cludes for a long time via the <CFINCLUDE> tag:

<CFINCLUDE TEMPLATE="file.cfm">

Here's the JSP equivalent:

<%@ include file="file.jsp" %>
Simple. Enough said.

Commenting Your Code
All code is commented (or should be). Here's a CFML comment:

<!--- This is a comment --->
And here's a JSP comment:
<%-- This is a comment --%>

Your JSP code, like your CFML code, can also use simple HTML comments if you want the comment to be sent to the client.

Summary
We've just scratched the surface - but hopefully Java (via JSP) is looking just a bit less intimidating. We'll continue this topic in the next issue with a discussion on forms, URLs, database access, and more. We'll also look at how to invoke Java code from within CFML. Stay tuned.

About Ben Forta
Ben Forta is Adobe's Senior Technical Evangelist. In that capacity he spends a considerable amount of time talking and writing about Adobe products (with an emphasis on ColdFusion and Flex), and providing feedback to help shape the future direction of the products. By the way, if you are not yet a ColdFusion user, you should be. It is an incredible product, and is truly deserving of all the praise it has been receiving. In a prior life he was a ColdFusion customer (he wrote one of the first large high visibility web sites using the product) and was so impressed he ended up working for the company that created it (Allaire). Ben is also the author of books on ColdFusion, SQL, Windows 2000, JSP, WAP, Regular Expressions, and more. Before joining Adobe (well, Allaire actually, and then Macromedia and Allaire merged, and then Adobe bought Macromedia) he helped found a company called Car.com which provides automotive services (buy a car, sell a car, etc) over the Web. Car.com (including Stoneage) is one of the largest automotive web sites out there, was written entirely in ColdFusion, and is now owned by Auto-By-Tel.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

hello,

I read your article part1, where can I go on?


Your Feedback
sebastian mork wrote: hello, I read your article part1, where can I go on?
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE