Journal

Scripting - Part 2

PC Pro logo Posted: 1st June 2001 | Filed under: Press Articles, Technical
Author: Paul Ockenden
First Appeared in PC Pro 2001

Last month we looked at Perl, the oldest Web scripting language, and Visual Basic Active Server Pages, one of the most common. This month we'll conclude the tour of Web server scripting technologies by looking at ColdFusion, which is produced by Allaire, developer of the excellent HomeSite HTML editor package, and was once a main competitor to Microsoft's ASP (Active Server Pages) technology.

ColdFusion is still better than ASP in certain areas like content aggregation for portal-style sites, but in general it seems to have fallen out of favour with much of the Web development community. This makes building and maintaining a ColdFusion site costly - not because the software itself is expensive, but because programmers or Web agencies can be hard to find, and expensive when you do locate one.

Much like ASP, ColdFusion works by mixing HTML and script code within the same file, but the main scripting mode is different from ASP, as each scripting control or element is implemented in the form of an HTML-style tag. A loop, for example, would sit between <CFLOOP> and </CFLOOP> tags. However, for those who hate the idea of this tag-based scripting, ColdFusion also provides CFML scripting with a more JavaScript-like syntax. Here's a ColdFusion version of the font-size loop which we used to demonstrate ASP last month:


<CFLOOP INDEX="LoopCount" FROM="2" TO="6">
<FONT size=<CFOUTPUT>#LoopCount#</CFOUTPUT>>Font size
<CFOUTPUT>#LoopCount#</CFOUTPUT></FONT><BR>
</CFLOOP>

ColdFusion is still a good choice for building large, high-demand, high-availability Web sites, because it supports load balancing and automatic fail-over. This means your Web site can be shared across a number of servers, and any new visitor will get directed to the server that currently has the lowest loading, and should one server fail, the others in the server cluster will automatically take over. Once upon a time, ColdFusion was one of the few Web technologies that could manage this, but now many others can do it too.

That font loop example might look a little 'clunky' compared to many other scripting languages, but in other ways ColdFusion can be quite elegant. Take a look at the following example, which displays product details from a database. The ASP programmers among you will notice how simple this ColdFusion code is when compared to a typical VBScript database lookup loop, particularly in the way that the table gets built automatically. This can make for a tricky time when a designer is called in to make the page look pretty though, because it's hard to separate the cosmetic from the functional parts.


<HTML>
<HEAD>
<TITLE>A simple example</TITLE>
</HEAD>
<BODY>
<CFQUERY NAME="ProductList" DATASOURCE="products">
SELECT PRODNAME, SKU, PRICE FROM ProductList
</CFQUERY>
<H1>Product list</H1>
<CFTABLE Query="ProductList">
<CFCOL HEADER="Product" TEXT="<B>#PRODNAME#</B>">
<CFCOL HEADER="Order Code" TEXT="#SKU#">
<CFCOL HEADER="Price" TEXT="#PRICE#">
</CFTABLE>
</BODY>
</HTML>

JavaServer pages

A particularly interesting server-side scripting technology is JSP (JavaServer Pages), from Sun (http://java.sun.com/products/jsp). Strictly speaking, JSP is a specification rather than a product, and there are around a dozen or so vendors that can supply you with a JSP engine to sit on your server. In terms of programming, at first glance JSP looks remarkably similar to ASP, using the same <% and %> script tags, and even using the <=variable%> syntax for output.


<HTML>
<HEAD>
<TITLE>A simple JSP example</TITLE>
</HEAD>
<BODY>
The date is <%=new Date()%>
</BODY>
</HTML>

Behind the scenes, however, JSP is something quite different, very elegant and very clever. Whenever a JSP page is requested from the server, a Java servlet class gets generated for the code within the page, and this servlet is then executed, mixing its output with the HTML as appropriate. You might ask why use JSP at all then - why not simply do the whole thing with servlets? Well, first it means the user interface part of your page is written directly in HTML, not as output from the servlet, which enables you to work with your normal HTML design tools. Second, when JSP is used, rather than recompiling the whole system whenever you make a change, the server takes care of this as and when required. The whole thing is much more suited to RAD (Rapid Application Development) than a typical servlet-based system. Another great advantage for any large- scale e-commerce operation is that JSP is an integral part of the Java subset for server-side applications (J2EE, http://java.sun.com/j2ee/overview.html) and that means such scripts can be quite easily integrated with your databases, order processing and messaging systems.

JSP users are often found going head to head with ASP in tech-head discussions in newsgroups, and we've even heard a lively debate in the pub over the relative merits of both systems. In our opinion, there's no doubt that JSP has a far steeper learning curve than ASP, but it also has a much higher ceiling - JSP certainly has the edge for high-end and complex systems. Both technologies are evenly matched for medium-scale applications, although ASP will probably be easier to apply. For small systems though, ASP is the clear winner. But as always, you'll need to factor in the skills of your existing staff when making any technology decisions. JSP had something of a rough birth, with all kinds of incompatibilities and syntax changes between the first few versions, but JSP 1.1 is starting to look like a mature product and deserves serious consideration. This is one we'll certainly be coming back to in future columns.

PHP

A scripting system that seems to be particularly in favour with the Linux/Apache clique is PHP (www.php.net). There's an unresolved and ongoing argument over what PHP actually stands for. One camp says Personal Home Page, while the others say it's a recursive acronym 'PHP, Hypertext Preprocessor'. But that's not what's important: what is, is that PHP is now a mature and well-respected technology. It originally used to find support mostly from the 'Anti-Microsoft' brigade, but now its message is spreading to a much wider audience. PHP is both open source and cross-platform - in addition to Linux, it runs on many other Unix versions and also on Windows servers. When used with the Apache server, it can be built in as a module, which makes it very fast. The default syntax for PHP is to use <? ?> tags to surround the scripting code, but it can also be configured to use ASP-style <% %> tags. The actual language itself has obvious roots in both C and Perl:


<HTML>
<HEAD>
<TITLE>A simple PHP example</TITLE>
</HEAD>
<BODY>
The date is <? echo date("l F d, Y"); ?>
</BODY>
</HTML>

Form handling in PHP is particularly simple and elegant. If, for example, you use a PHP script to process a form containing a text input box named 'fred', you can simply refer to the result as $fred within your code. PHP is also often matched up with MySQL, a GPLed (GNU Public Licenced) database system, and these two products in combination enable you to set up a very high-quality and low-cost server platform. On your next visit to PC Pro's Web site, try to spot just how many of its pages are built using PHP. Like JSP, PHP is a technology that will feature heavily in future columns.

Others

Another dynamic scripting environment is HAHTsite, pronounced 'hot site' (www.hahtsite.co.uk). This is a complete commercial application development and server environment, similar in many respects to Allaire's ColdFusion except that its native scripting language is based on standard Visual Basic - that makes the learning curve less steep for many users. HAHTsite has a lot of advocates, although it suffers many of the same deployment problems as ColdFusion.

Oh, and there's no need to actually restrict yourself to scripting languages at all. There's no reason why a Web site couldn't be written entirely in C, Delphi, Fortran or whatever programming language you feel comfortable with. All it has to do is follow a set of rules whereby it listens for requests and spits out appropriate chunks of HTML, plus image files. Okay, for a typical corporate site it would be incredibly silly to write it as a C executable, but for a Web front end to a piece of machinery it would be ideal. If you have a networked printer somewhere on site, try connecting to it with your Web browser - you might find that it contains a little Web site.

Finally, to finish off, there are two more scripting languages that are well worth checking out, namely Python and Tcl (Tool control language). Both, at first glance, appear more low level than the other languages we've shown you, but don't let that put you off. Python is nice in that it's a very readable language (much more so than Perl) and also has strongly object-orientated features. What's most weird about Python is that as a programming language it seems to attract very few critics, despite it having a feature that will be alien to many coders: its program structure is defined by indentation. There's no 'end if', no brackets, just indentation. Take a look at www.python.org to find out more.

Tcl is a general-purpose scripting language with a huge breadth of functionality, but which is also eminently suited to the provision of dynamic content. Two good places to start investigating Tcl are www.scriptics.com and particularly www.arsdigita.com/books/tcl/index

That concludes this preliminary round-up of scripting technologies. Hopefully, we've given you a broad overview of what's out there, and a taste for some of the things you can start to do with server-side scripting. Over the coming months we'll be looking at solutions that use many of these languages, as well as providing any handy hints and tips we come across that you may find useful. On that last point, please feel free to email us with any gems you think we ought to share with the readers of this column.