Related Topics: ColdFusion on Ulitzer, Apache Web Server Journal, Java Developer Magazine

CFDJ: Article

Three Flavors

ColdFusion, ASP, & Java Server Pages

Ever wanted to compare how ColdFusion, ASP and Java Server Pages perform the same actions? In this article David writes the same small application in all three and compares the coding and syntax.

Which technology?
As developers we all ask ourselves the same questions over and over again: What technologies and languages should I use? What's better, ColdFusion or ASP? What is Java Server Pages? Which will pay me the greater return for my efforts and time?

At the moment the three most popular web scripting languages are arguably Macromedia's ColdFusion, Microsoft Active Server Pages, and Sun's Java Server Pages. To provide some insight into the different syntax and coding styles of these languages, in this article we'll develop the same small web application in each of the three technologies.

The application
Time and time again we all use a simple database table maintenance application and our application will maintain a table containing a single field named furniture.

I first developed the application in ColdFusion and then mimicked it in ASP and JSP. Keep in mind that the scripts are quick and dirty, by no means complete and contain little or no error handling. The minimum development environment needed to create any of these scripts is a text editor.

Our application program is broken up into three separate scripts: three files in each set. The file extensions are .cfm for ColdFusion, .asp for Active Server pages, and .jsp for Java Server Pages.

All nine scripts — three for ColdFusion, three for ASP, and three for Java Server Pages — and a sample database can be downloaded.

furniture-l displays a list of records from the furniture table with hyperlinks that allow a user to add, edit, or delete records.

View the code for Furniture1.cfm

View the code for Furniture1.asp

View the code for Furniture1.jsp

furniture-d handles the record detail for both adding and editing.

View the code for FurnitureD.cfm

View the code for FurnitureD.asp

View the code for FurnitureD.jsp

furniture-p handles the processing functionality.

View the code for FurnitureP.cfm

View the code for FurnitureP.asp

View the code for FurnitureP.jsp

We'll examine the furniture-l script in detail and then summarize and compare most of the syntax differences for all the three scripts.

Setting up your environment
You'll need an Access database named House. The database should contain a single table named furniture with one field named furniture. The data field should have a text data type and the size doesn't matter for our purposes. Next, you'll need a data source named house that points to the House database.

The furniture-l script — the first to be called in our application — queries the database and then displays a list of the results in a table.

Listing 1 shows how this is accomplished using ColdFusion.

View the code for Furniture1.cfm

ColdFusion uses the <cfquery> tag to query the database. The <cfquery> tag is extremely useful and one of ColdFusion's most frequently used tags. <cfquery> can accommodate different parameters such as dsn, username, password and more.

In our case we are only providing the dsn, the name of the query variable, and the sql statement. The results of the query are then stored in the variable GetData.

To display the output we use the <cfoutput> tag. Anything contained between the opening and closing <cfoutput> tags will be repeated for each record in the result set.

The #variable# is ColdFusion's way of outputting variables and must be placed between the opening <cfoutput> and the closing </cfoutput> tag.

ColdFusion server then interprets the tags and spits back the associated html to the browser.

Figure 1 – The ColdFusion Rendering

In Listing 2 we have the same code written in ASP (Active Server Pages). You can use either IIS or Personal Web Server as your server.

View the code for Furniture1.asp

ASP contains server side scripts, surrounded by the delimiters <% and %>. The Web server interpets the scripts and returns the html to the browser.

First we create a connection and recordset object. The results of the query are then returned and stored in the rs recordset object.

We then loop through the resultset using a while loop and the movenext method.The data is then displayed using the <%=rs("Furniture")%> statement. Lastly we free up any resources.

Figure 2 – The ASP Rendering

Last but not least is JSP (Java Server Pages) and the code is found in Listing 3. A point to note with JSP is that you're not tied into a specific server as with ColdFusion (ColdFusion Server) and ASP (IIS or Personal Web Server).

View the code for Furniture1.jsp

In JSP, the code is surrounded with <%@ and end with %&> tags.

At the top of each JSP is a page declaration tag which may contain different attributes. The import attribute contains a list of packages or classes that need to be imported for the compiler. In our case we need the sql package.

In Java any statements that might throw an exception must be caught with an exception handler. The statements that might throw the exception are coded within a try block and any exceptions are handled within a catch block.

All Java statements end with a semicolon.

One connects to a database by (1) loading the driver and (2) making the connection.

In our case we load the JDBC-ODBC Bridge driver. The connection returned by the method DriverManager.getConnection is an open connection to the database.

The result set is then returned in the ResultSet object rst.

Next, we cycle through the result set using a while loop and the method.

The data is then output by setting the resultset(Fieldname) to a variable and then displaying the variable. The connection and statement objects are then closed.

Figure 2 – The Java Server Pages Rendering

Syntax comparisons
Here are some syntax comparisons between languages.

  ColdFusion Active Server Pages Java Server Pages
Minimum Needed to Run Application ColdFusion Server Personal Web Server:
Java Server — Our example used Apache Tomcat:
Comment <!--- Comment ---> '// Comment */Comment/*
Tags Open With <CF..> <% %> <% %>
Querying the Database <CFQUERY DATASOURCE="
     HOUSE" NAME="GetData">
   FROM Furniture
<% Set conn = Server.CreateObject("ADODB.Connection")
   conn.Open "DSN=House"
   Set rs = conn.Execute("SELECT * FROM FURNITURE")
   Connection mCon = DriverManager.
     getConnection ("jdbc:odbc:HOUSE");
   Statement stm = mCon.
   ResultSet rst = stm.executeQuery("SELECT * FROM FURNITURE");
Displaying a Field Variable <cfoutput query="GetData">
<%=rs("Furniture")%> <% String s1 = rst.getString ("Furniture"); %><%= s1 %>
Jump to Another Page <cflocation url="furniture-l.cfm" addtoken="No"> <% Response.Redirect "furniture-l.asp" %> <% response.
   sendRedirect("furniture-l.jsp"); %>
If then else <cfif PROCESS eq "A" >Adding <cfelse>Modifying
<%If Request.form("PROCESS") = "M" then%> Adding<%Else %>Modifying<%End if %> <%if (PROCESS.equals("A")) {%> Adding <% } else { %> Modifying <% } %>
Get Variable Data From an URL URL.PROCESS Request.QueryString("PROCESS") request.getParameter("PROCESS")
Get Variable Data From a Form FORM.FURNITURE Request.Form("FURNITURE") request.getParameter("FURNITURE")
Ease of Use
(A = easiest)
(A = least)


In this article we examined a small application written in ColdFusion, ASP and Java Server Pages. In summary I think the easiest language is ColdFusion, the most widely used is ASP, and the most portable is JSP. As far as which pays the most, well — as usual — it's all relative but you'd surely benefit by having all three in your resume. I hope I was able to provide some insight into the three Web scripting languages.


Windows2000 Magazine: ASP

Sitepoint: Getting Started with ASP

ASP Resource Index, Tutorials & Code Snips

ASP Samples

Java Server Pages Tutorial

More Stories By David Adress

David Adress puts his strong knowledge in ColdFusion, Powerbuilder, Visual Basic and Oracle to work for Intrasphere Technologies, Inc. a leader in providing a full range of services for engineering enterprise-class web applications. David has acquired more than 10 years' experience in the computer field since receiving a B.S. in mathematics/computer science from Touro College (New York).

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.