Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

CGI Programming Unleashed

Chapter 24 -- Java and JavaScript as Alternatives to CGI

Chapter 24

Java and JavaScript as Alternatives to CGI


Java and JavaScript are two languages you can use as an alternative to using CGI applications. The CGI specifications are respected by programs that run on the server side-the Web server. But Java and JavaScript are technologies that allow execution of special programs or actions on the client side. So, Java and JavaScript are not related in any way with the CGI specifications, but its study as an alternative to CGI programming is interesting and can be useful in a lot of applications. This chapter presents both technologies and compares them to CGI. We cover the usefulness of each programming alternative in the development of Web applications.

References to resources on the Internet that cover Java, JavaScript, and the CGI specification are also given so that you can explore these technologies further and make the best choice when you plan a Web application.

Java: Bringing More Dynamics to the Web

Java is an object-oriented programming language developed by Sun Microsystems and is somewhat similar to C++. Its development started back in 1990, but the official announcement of the Java language and support material was made in 1995. Java brings more interactivity to World Wide Web documents because it allows one to do things that were otherwise impossible or hard to do with current Web technology. In fact, because Java is a programming language, it enables you to create both standalone programs and programs that can be embedded in Web pages using special tags. By giving the possibility of inserting a program inside a Web page, Java opens a lot of new horizons to information presentation on the Web.

With Java, Web pages can become real applications instead of plain documents containing static information. Java programs (commonly called Java applets) embedded in HTML pages with the <APPLET> tag are always executed on the client side-the browser. It is not the program itself that you embed in the documents but the program name along with its parameters.

You do not have to install each program you execute. In fact, it's up to the browser to interpret Java applets and execute the actions. The browser requests a server for a document, requests the associated applets, and executes them. Java applets are not restricted by network bandwidth (once they are transferred to the local computer, they are executed as fast as the computer allows it) or server overload, as CGI applications may be.

The difference between Java and browser plug-in applications is that you do not have to install a plug-in for each different type of information you want to display (if you consider using a new type of information). With Java, you request a program each time-not a plain data file of various formats-that may do virtually anything inside a Web page.

Imagine that a new image format has appeared and you do not have a browser that supports it. If you connect to a normal page with the image embedded, you will not be able to view it unless you download a browser plug-in or helper application for your browser. But if you connect to a page containing a special Java applet, it is the Java applet itself that runs on your computer and is responsible for the image retrieval and display. With a programming language like this embedded in Web pages, you can create lots of different applications: animations, spreadsheets, automatically updated graphics embedded in a page, games, and so on.

A Java applet can be run on different computer platforms, so it cannot be compiled for a particular platform. A solution would be the interpretation of the Java program, but that could result in very slow applications. Java is a hybrid solution between these two. In fact, a Java applet is compiled to a special byte code-an intermediary representation of the program-and it is the byte code that is executed by the Java "interpreter" (it does not interpret Java sources; it interprets Java byte codes). This way, every machine with a Java interpreter can run a Java program, even if it is developed in a different platform and is provided by a different Web server.

A Java program in source be compiled before execution by the client. The name of the compiled version becomes something.class. One typical programming example is the "Hello World!" string. Let's look at the source:

Class HelloWorld [
    public static void main (String args[]);
    System.out.println("Hello World!");

This program can be compiled by using the Java compiler (javac), distributed by Sun with the Java Development Kit.

Java is currently supported by the Netscape and HotJava browsers, but version 3.0 of Microsoft Internet Explorer (the final version, after the beta test phase, will be out by the time you read this) will support it, as well. The HotJava browser is a Java-compatible browser developed by Sun and written in Java! Remember the chicken and the egg story?

You can find out more about Java, the Java Development Kit, and some Java applets at the following URLs:

What Is JavaScript?

JavaScript is not another version of the Java programming language, nor is it any new project of the Java team. Following the release of Java, Netscape continued to push the development of the World Wide Web further and released JavaScript as another means of adding client-side programs to Web documents.

Netscape initially called JavaScript Mocha and then LiveScript. Its aim was to create a programming language that could be embedded in Web pages. Unlike Java, an HTML page does not have an applet tag referencing the actual Java program, but the program itself is embedded in the HTML source, surrounded by the <SCRIPT> tag! Programs in Web pages are called scripts instead of applets or applications.

JavaScript was intended mainly to help recognition and response to user events such as mouse clicks or forms submissions (a JavaScript program can check for the user's input before it is sent to the server).

JavaScript is also executed on the client-side, therefore lessening the load on a Web server. JavaScript programs are usually less complex and smaller than Java applets and do not have to be compiled before execution. They can be written directly in a Web page and executed (interpreted) by the browser that requests it. JavaScript is currently supported by the Netscape browser. JavaScript programs are embedded in HTML pages through the use of the <SCRIPT LANGUAGE="JavaScript"> ... </SCRIPT> tags. The typical "Hello World!"program is presented in Listing 24.1.

Listing 24.1. The Hello World! program.
document.write("Hello world!");

Differences between Java and JavaScript

Java and JavaScript have some similarities but more differences than their names may reveal. JavaScript is a scripting language and HTML-page oriented (the script is embedded in the HTML source), while Java is a complete programming language that can be used in and outside the Web world (standalone programs may be developed using Java, for example).

On one hand, Java applets are only referenced inside the HTML source of a page and executed inside the browser's window. JavaScript scripts are actually embedded in the HTML source and are also executed by the browser on the document's window.

Java programs consist of classes and respective methods, and their objects are declared and safely typed. JavaScript is a smaller language with an easier syntax that has some data types already built-in.

Another difference comes from the execution strategy. JavaScript programs are interpreted while Java applets must be precompiled before execution (a pseudo-interpretation, in fact). Object references in JavaScript are checked at runtime while in Java they exist at compile time.

Both languages pretend to be secure, and Web programs developed with them cannot (or should not), in particular, write to the hard disk. Java also has security features concerning network functioning. Java seems to comply with security issues better than JavaScript. You can check out for a list of known JavaScript bugs.

Comparison of CGI and Java/JavaScript

CGI applications are closely related to a Web server, both-application and server-respecting the CGI specifications. Also, CGI applications are executed on the server side, while Java or JavaScript programs are executed on the client side.

In general, applications that require a lot of processing on a server, such as accessing or controlling a database, gateways, or other Internet services or protocols, are better developed with CGI applications. But if you plan to add some dynamics on Web pages, execute animations, graphics, movements, or other features, you will probably be better served by Java and possibly JavaScript. Being executed on the server side, CGI applications put some extra load on a Web server instead of on the client side, as Java and JavaScript do.

Due to its architecture and available system tools (that can be used by a CGI application), a CGI application can easily use the hard disk of a server or initiate network connections on the Internet. Java and JavaScript programs are not able to access directly the server's disk or initiate network connections easily enough (or at all in the case of JavaScript). CGI applications, in fact, are not limited to one programming language. They are limited only by the CGI specifications, which are general enough to allow the use of any programming language, such as C, Perl, C++, and so on, and consequently, the characteristics of any of these languages. Java applets are not an interface definition but are actual programs.

Communication between Java and CGI

Java and CGI applications often serve different needs and can be complementary tools. In many situations, you may choose to develop your application by using CGI specifications or by using Java. But it is also possible for you to use both technologies because how they complement one another can be of great use. Fortunately enough, CGI applications may call Java applets, and vice versa, so that both can communicate and offer better applications on the Web.

Java to CGI

Java applets can call CGI applications with arguments, just like a simple HTML page. This works for CGI calls with the GET method:

cgiScript = "";
getAppletContext().showDocument(new URL cgiScript));

CGI to Java

Imagine that you'd like users of your pages to pass arguments to Java applets in the same way they do for CGI applications, like this:

One such possibility is to create an applet call with the following arguments:

<APPLET CODE=example.class width=400 height=300>
<PARAM NAME=quantity VALUE=5>

A more elegant solution is to send arguments to a CGI application in the normal way and use this application as a wrapper to identify the arguments and generate the proper applet call.

Java and JavaScript

Java and JavaScript are expected to communicate between each other in future versions of the Netscape browser. A JavaScript function could capture an event (mouse click, form submission, and so on) and pass commands to a Java applet, or a Java applet could generate events for JavaScript to capture, for example.


An interesting possibility for JavaScript is the use of server-side includes (SSI) to insert modules at some place in the code:

<!--"include file="lib.js"-->

The page is pre-parsed by the server, so each SSI tag is replaced by the server with the associated program results or file. This way, modules (pieces of JavaScript code) can be created and organized in different files and included in a page only when needed. Instead of including a file, the SSI tag could execute a program that would output the code for the modules. These modules could, for instance, make use of some of the environment variables passed from the server to the SSI script.

Each Technique Has Its Place

When developing Web applications, one has several choices on the horizon: Java, JavaScript, and CGI applications. CGI applications can also be developed by using one or more of the various computer languages available, depending on the aim of the program and on the experience one has.

Java is not the ultimate application language, and neither is JavaScript. You should learn to live with all these possibilities, putting more emphasis on Java and CGI applications. Java is client-side oriented and well suited for custom user interfaces or applications that can depend solely on the local computer resources (such as animations, calculations, games, and so on). CGI applications are well suited for most server-side dependent actions, such as accessing databases, decoding form contents, or using gateways to other protocols and services on the Internet.

Also, Java is being used to create many special effects and more dynamic Web pages. One such example is presented in Figure 24.1. It is a scrolling message applet, which is useful to show users important messages or to catch their attention to some event. The same effect is also possible with JavaScript, and its source is presented in Listing 24.2.

Figure 24.1:The scrolling message applet.
Listing 24.2. The JavaScript scrolling message source.
<TITLE>Scroll bar JavaScript page</TITLE>
var MyString="Look at this. Isn't it simple?          "
var timer=0
function Slide() {
<BODY onLoad="Slide()">
<H1 ALIGN=center>JavaScript scroll bar page</H1>
<FORM NAME="box" onSubmit="0">
<INPUT TYPE="text" NAME="boxtext" SIZE=40 VALUE="">

Another funny Java applet is the visitor counter. Instead of a static counter that displays the number of visitors to a page, the dynamic Java counter always shows updated results without user intervention (such as pressing the reload button); it really turns while you're visiting a page! Check out for an example of this counter. The counter is displayed in Figure 24.2.

Figure 24.2:An auto-updated counter.

Is there really a need to learn Java, JavaScript, and CGI programming? The answer is yes. If you want to do serious work in Web programming, you should probably learn the CGI specifications and some common CGI applications languages, such as Perl or C. Java can enhance your pages, make them more attractive, or help you develop some serious applications, so it would be nice to learn it, too. Many people think it is the future of programming in general, so the time passed learning Java will surely not be wasted time. JavaScript is a simpler language, Netscape-proprietary, that enables one to create some special effects on Web pages, without the need of a more complex Java program and a compilation process.

Will Java make CGI applications obsolete and, for example, eliminate the use of Perl? Surely not. CGI applications are the most suitable solution to many problems. Perl text-parsing and handling, for example, are far superior to Java's, while Java applets can handle other functions that Perl programs cannot (because of the server-side execution, for example).

Future Improvements

Most of the time, network available bandwidth is the bottleneck for every Web application. Sometimes, even the small Java applets suffer from slow downloading. Java is quite slow in computers in which normal compiled programs run fast (such as a low-end 486). We expect that both network bandwidth, to allow Java network-intensive applications, and computational power will make Java a widespread language. Also, there are not many tools (compilers, debuggers, viewers, and so on) available today to support Java development. Some exist, but the future will probably bring us improved development tools.

Where to Get More Information

See the following for more information:

JavaSoft Home Page



Java FAQ Archives

JavaScript Authoring Guide

JavaScript Index

JavaScript Resource Center



Java and JavaScript are two alternatives to CGI programming. This chapter presented Java and JavaScript functionality and covered both its similarities and its differences. Java is a complete programming language and can substitute for CGI applications in many cases, while JavaScript is a nice scripting language that can help one put interesting effects on an HTML page with a simple embedded program.