Browse DevX
Sign up for e-mail newsletters from DevX


Rediscover (and Structure) CGI with a Simple Link Technique : Page 2

CGI is the most widely used protocol for Web programming, but it lacks basic programming constructs such as function calls. You can work around these weaknesses by encoding function calls directly into Web links easily creating active links—and you can do it in PHP, Perl, JavaScript, and Java.

Something Better
If you've been Web programming for a while, you may be used to these problems. However, there are alternatives. While it's beyond the scope of this article to consider all the possible solutions (see the related resources), this article covers one possibility—something I call "active links."

An active link is a kind of function call that has been encoded as a Web link on a Web page. When the user clicks on it, the function is called. It's as simple as that.

Here is how I'd like an active link to look—again, in Hypothetical Web Language:

$link = activeLink( processOrder( $account, $method ) );
<a href="$link">Click to process order!</a>
In the above example, you're not calling processOrder(). Instead, you're freezing a call to processOrder() and stuffing it in a Web link. When the user clicks on that link, the order is processed.

Most commercial languages use convenient syntax like this. Programmers have to juggle things around a little bit to get it to work. But the syntax above is the ideal—after all, succinctness is power.

Figure 1. The User Interface of the Mult Program: Clicking on a link will give you the product of the two numbers.

The following sections, describe a single, simple program, written in multiple languages—Java, JavaScript, Perl, and PHP. In each case, it will be as convenient as possible to create and use an active link, and you'll develop the libraries necessary to make it work.

The Example Application
In order to focus on programming-language issues, the example application is very simple—it's a multiplication table, called Mult. Figure 1 shows the user interface.

If you click on any of the links, you get the answer:

Figure 2. Click the Links: The result of clicking on a multiplication link in the Mult program.

In each language, the implementation consists of three parts:

  • The multiplication routine, called mult(), which takes two integers and returns the answer.
  • The main page, which renders the multiplication table filled with active links.
  • The infrastructure, which "freezes" a call to mult(), and then "thaws" it when the user clicks on a link.
The idea here is to make the first two parts very convenient, because they comprise the bulk of all Web programming. The third part involves a bit of trickery, but that's okay—you only have to do it once. In a sense, you're trying to take the messiness described above in the shopping-cart example and remove it from the day-to-day code you have to write. To do this, it must be hidden in the infrastructure.

The following sections demonstrate how this is implemented in the four languages.

Thanks for your registration, follow us on our social networks to keep up-to-date