Accessibility
Raymond Camden

Raymond Camden

coldfusionjedi.com

Created:
26 January 2009
User Level:
All
Products:
Dreamweaver

What server language should I use?

Note: This article was originally written by Matt Brown in 2004 and updated by Raymond Camden with the help of Derek Perez and Martin Olivera.

Taking your website to the "next level" usually means you need to connect your website to a database and create dynamic data. Accessing databases from your site opens your business to a whole new world of alternatives. You can create easily updatable catalogs, customer surveys, shopping carts, and customer management systems. However, before heading down that road you have to consider a series of questions.

One of these questions is, "What server language should I use to make a dynamic website?" Your options include ASP, CFML, PHP, JSP, Lasso—sometimes it all seems like an alphabet soup. However, choosing a server language is not that difficult.

In general, you can divide server languages into tag-based languages (like HTML) or script-based languages (like JavaScript or Java). From my experience, if you are coming at the issue from a designer's point of view, you might have more luck with tag-based languages. If you like to script or program, you are probably going to find script-based languages easier to use.

Tag-based languages

Adobe ColdFusion (CFML)

The most common of the tag-based languages is Adobe ColdFusion Markup Language (CFML). To write CFML, you can use a text editor or Dreamweaver CS4. A piece of CFML looks similar to regular HTML, except there is a new set of tags. ColdFusion tags allow you to connect to a database, print data from a database, and do all the things that you need to do with data. For example, you can repeat data over a region or create insert and edit forms that interact with a back-end database. To learn more about ColdFusion, visit the ColdFusion product page, where you can download a free trial.

Just for comparison of the languages, here is a sample CFML page that accesses a database and prints out all the first and last names, each on their own line.

<cfquery name="GetEmployees" datasource="exampleapps">
     SELECT firstname, lastname FROM tblEmployees 
</cfquery>
<html>
<head>
     <title>Untitled Document</title>
</head>
<body>
     <cfoutput query="GetEmployees">
     #FirstName# #LastName#<br>
     </cfoutput> 
</body>
</html>

The first part of the code, the <cfquery>, accesses the database and tells ColdFusion to grab all the information from the tblEmployees table. Next comes the normal HTML head of the document and the body. In the body there is a <cfoutput> tag which tells ColdFusion where to insert the data from the query. The <cfoutput> tag has a placeholder for the first name, the last name, and a break. When the page prints out to the browser, you will see each first name and last name with a break between them.

It should also be mentioned that you can write ColdFusion in a script-based version of the language as well. This form of ColdFusion is written in CFSCRIPT blocks and isn't quite as full featured as the tag-based version.

Lasso

Another tag-based language is Lasso, from Blueworld. Lasso looks a little less like HTML, but is still very easy to use. To write Lasso code, you would use a text editor. You can also use Dreamweaver with the Lasso Studio for Dreamweaver, which is available from Blueworld. The Blueworld site includes a set of white papers that compare Lasso to other languages.

If you use either of the two tag-based languages, you will need to buy the server software or find an Internet service provider that offers that server to host your pages.

Script-based languages

PHP

PHP is sort of a hybrid of tag- and script-based languages. It is widely supported because it is an open source language. Implementing PHP does not require buying a server. There are many PHP resources available in the form of books and discussion groups.

PHP is one of the fastest-growing languages on the web. To write PHP, you could use a text editor or Dreamweaver CS4. You can also find information about PHP directly from the people who created it on the PHP home page.

The following is an example of PHP that connects to a database, gets first and last names, and then repeats them on new lines.

<?php require_once('file:///C|/wwwroot/mattcon.php'); ?>
<?php
     mysql_select_db($database_mattcon, $mattcon);
     $query_Recordset1 = "SELECT * FROM pet";
     $Recordset1 = mysql_query($query_Recordset1, $mattcon) 
     or die(mysql_error());
     $row_Recordset1 = mysql_fetch_assoc($Recordset1);
     $totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php do { ?>
<?php echo $row_Recordset1['name']; ?> 
<?php echo $row_Recordset1['owner']; ?> <br>
<?php 
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> 
</body>
</html>
<?php mysql_free_result($Recordset1); ?>

The first block of PHP code is a link to an external file that contains the details of the connection to the database. Then there is a block of code that starts a connection to the database.

After the connection is made, there is a query to the database that selects everything from the pet table and returns a result set. (This example uses the sample database from MySQL.) The next block sets up two variables used in the repeat function later in the code. Inside the body, the code retrieves each pet name then the owners. It then repeats it as many times as there are records. Finally, the connection is closed.

Active Server Pages (ASP)

Although its use is in decline due to the appearance of ASP.NET, ASP is still a very popular choice, since it is supported by default in all installations of Internet Informations Services (IIS). It is very well documented by Microsoft and other third parties. Since it's been around for quite a long time, you will be able to find a lot of online samples and tutorials. Due to the popularity of Windows-based servers, you will need to be very careful to keep up to date with patches and fixes that are released very often from Microsoft.

Here is an example of ASP that retrieves employee names and displays them on a web page, just like the CFML example above. (You may notice that there are a few more lines of code in the ASP example than in the ColdFusion example.)

<%@LANGUAGE="VBSCRIPT"%>
      <!--#include file="Connections/ASPconnection.asp" --> 
      <%
            set Recordset1 = Server.CreateObject("ADODB.Recordset")
            Recordset1.ActiveConnection = MM_ASPconnection_STRING
            Recordset1.Source = "SELECT * FROM tblEmployees"
            Recordset1.CursorType = 0
            Recordset1.CursorLocation = 2
            Recordset1.LockType = 1
            Recordset1.Open()
            Recordset1_numRows = 0
     %>
     <%
            Dim Repeat1__numRows
            Repeat1__numRows = -1
            Dim Repeat1__index
            Repeat1__index = 0
            Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
     %>
  <html>
  <head>
  <title>Untitled Document</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
  <% 
     While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF)) 
  %>
  <%=(Recordset1.Fields.Item("FirstName").Value)%> ,
  <%=(Recordset1.Fields.Item("LastName").Value)%><br> 
  <% 
     Repeat1__index=Repeat1__index+1
     Repeat1__numRows=Repeat1__numRows-1
     Recordset1.MoveNext()
     Wend
  %>
  </body>
  </html>
  <% Recordset1.Close() %>

The first part of the ASP code above defines the script language, in this case, VBScript. Then there is a link to a connection where the details of the connection are stored. The connection details are stored externally and called by a server-side include so that the same connection can be used across many pages.

The next block of ASP code initiates the connection to the database. It makes a query to select everything from the tblEmployees and sets a number of preferences for the connection. The next block of code sets up a script to repeat the code for each entry returned from the query. The block beginning with While grabs the first name and the last name and puts a break after each entry and repeats for as many times as there are entries returned from the query. At the end, the code for the connection is closed.

JavaServer Pages (JSP)

JSP is also a scripting language, but unlike ASP, it is designed specifically to work with very large, high volume sites that connect to enterprise databases. It has a set of tools called JavaBeans and Enterprise JavaBeans (EJBs) that are designed to move all the scripting into libraries. These libraries are linked to the page rather than placing the script on the page. Libraries allow you to easily change the logic of the application without having to change every page. To write JSP, you could use a text editor or Dreamweaver CS4. (Note: Dreamweaver supports JSP coding through code hinting and color coding.) To read more about JSP, visit the Sun JavaServer Pages website. There are also a number of books on JSP available from your favorite independent bookseller.

A sample of JSP to open a connection to a database and then repeat the first and last names from a table looks like this:

<%@page contentType="text/html" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="file:///C|//wwwroot/mattconn.jsp" %>
<%
     Driver DriverJSPDemo = (Driver)Class.forName(MM_mattconn_DRIVER).newInstance();
     Connection ConnJSPDemo = DriverManager.getConnection
     (MM_mattconn_STRING,MM_mattconn_USERNAME,MM_mattconn_PASSWORD);
     PreparedStatement StatementJSPDemo = ConnJSPDemo.prepareStatement("SELECT * FROM dbo.employee");
     ResultSet JSPDemo = StatementJSPDemo.executeQuery();
     boolean JSPDemo_isEmpty = !JSPDemo.next();
     boolean JSPDemo_hasData = !JSPDemo_isEmpty;
     Object JSPDemo_data;
     int JSPDemo_numRows = 0;
%>
<%
     int Repeat1__numRows = -1;
     int Repeat1__index = 0;
     JSPDemo_numRows += Repeat1__numRows;
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<% while ((JSPDemo_hasData)&&(Repeat1__numRows-- != 0)) { %>
<%=(((JSPDemo_data = JSPDemo.getObject("fname"))==null || JSPDemo.wasNull())?"":JSPDemo_data)%>
<%=(((JSPDemo_data = JSPDemo.getObject("lname"))==null || JSPDemo.wasNull())?"":JSPDemo_data)%> 
<% Repeat1__index++; JSPDemo_hasData = JSPDemo.next(); } %>
<br>
</body>
</html>
<%
     JSPDemo.close();
     StatementJSPDemo.close();
     ConnJSPDemo.close();
%>

The first block of code for JSP imports the Java functions to handle Structured Query Language (SQL). Then there's a link to an external file that stores the details of the connection to the database.

Next, there is a block of code that specifies the database driver and starts a connection to the database. After the connection is made, there is a query to the database to select everything from the dbo.employee table and return that as a result set. (This is not the same database as the previous example, but it has similar content.)

The next block sets up the script to repeat the data. Inside the body, the code gets the first name, then the last name, and then repeats it as many times as there are records. Finally, the connection is closed.

Ruby on Rails

Note: I'd like to thank Derek Perez for providing this section.

Rails is an open source full stack environment for building web applications with Ruby. Ruby's dynamic capabilities provide a very expressive language experience for developers, resulting in a powerful toolkit with a very straightforward framework. One of the most important libraries (that can be used independently from Rails) is ActiveRecord. ActiveRecord provides a robust object-relational mapping system for database-driven applications written in Ruby.

<html>
<head>
    <title>Untitled Document</title>
</head>
<body>
     <!-- equivalent SQL: SELECT * FROM `employees` -->
     <% Employee.all.each do |employee| %>
     <!-- the iterator provides access to a full class Employee object and its properties -->
     <%= employee.first_name %>, <%= employee.last_name %> <br/>
     <% end %>
</body>
</html>

ActiveRecord provides a complete database abstraction for every type of query, allowing the code above to work regardless of the database platform selected. Ruby's original interpreter (known as MRI) is written in C and is easily deployed using Phusion Passenger and Apache 2, but can also be deployed using Java in combination with JRuby and Tomcat.

Both Ruby and Rails ship with the latest OS X 10.5 (Leopard) operating system, and also have support for Microsoft Windows.

The best approach for choosing a server language is to take a quick look at each language. After evaluating them a bit, you'll be able to tell which one is most suitable for your needs.

Where to go from here

To get started with building dynamic sites and setting up a particular development environment, refer to the following tutorials and documentation:

Note: Dreamweaver supports PHP, ColdFusion, and ASP through dedicated user interfaces, code hinting, and code coloring. It also supports ASP.NET and JSP coding through code hinting and color coding.

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

About the author

Raymond Camden is a software consultant focusing on ColdFusion and RIA development. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books including the ColdFusion Web Application Construction Kit and has contributed to the Fusion Authority Quarterly Update and the ColdFusion Developers Journal. He also presents at conferences and contributes to online webzines. He founded many community web sites including CFLib.org, ColdFusionPortal.org, ColdFusionCookbook.org and is the author of open source applications, including the popular BlogCFC (www.blogcfc.com) blogging application.Raymond can be reached at his blog (www.coldfusionjedi.com) or via email at ray@camdenfamily.com. He is the happily married proud father of three kids and is somewhat of a Star Wars nut.