|
Comments
|
Today's Top SOA Links
BF on CF Java for CFers Part 1 of 3
Java for CFers Part 1 of 3
By: Ben Forta
Oct. 30, 2000 12:00 AM
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.
Introducing JSP 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 <%! %> 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
<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
<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
<CFLOOP FROM="1" TO="10" INDEX="i"> Here's the JSP equivalent:
<% for(int i=1; i<10; i++) { %> 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
<CFINCLUDE TEMPLATE="file.cfm"> Here's the JSP equivalent:
<%@ include file="file.jsp" %>Simple. Enough said.
Commenting Your Code <!--- 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
Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||