Skip to main content.

Web Based Programming Tutorials

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

Creating Web Applets with Java

Creating Web Applets with Java cwa01fi.htm

Previous Page TOC Next Page

— 1 —
What Is Java?

Java is perhaps one of the most talked about advances in Internet technology since the World Wide Web. People in many areas of computing are discussing Java and how it will change the face of computing. No doubt, Java will change the way people use the Internet and networked applications. Java has introduced many ideas to the World Wide Web arena, and those ideas represent some exciting changes to the Internet and computing.

Because the hype surrounding Java has caused some confusion, this chapter explains exactly what Java is and what it is not. This chapter discusses the basics of Java in order to give you a clearer picture of the types of programs you can produce with it and how those programs function on the Internet. Because many of Java's features have been the result of a very interesting development cycle, this chapter also includes history about the development of Java. After reading this chapter, you'll know where Java has been, where it is, and where it's going.

At the heart of all the Java talk is the Java programming language. Java is an object-oriented programming (OOP) language that uses many common elements from other OOP languages, such as C++, but it adds some enhancements to make programming Java easier. Like any other language, Java has a particular syntax, a structure for programs, and many supporting applications.

The Java Developer's Kit(JDK) contains all of the tools necessary to create applications (or Web applets) using the Java programming language, including the following:


The Java compiler


The Java debugger


The Java documentation program


The Java Virtual Machine


The Java applet viewer

Some of these Java components might not seem so obvious, such as the Java Virtual Machine. In using Java to program, you do not directly access the Java Virtual Machine. However, other commercial Web browsers that can run Java applets use it, as does appletviewer. All these elements are related (see Figure 1.1) or are directly linked to Java and using it on the World Wide Web.

Figure 1.1. The relationships between Java's various components.

A Brief History of Java

Java began its life in 1990 as a new language called Oak. Sun Microsystems had established a project group code-named green to develop new products and expand Sun's markets. Oak was originally designed for a personal digital assistant called *7 that Sun intended to market with a seamless graphical user interface.

The *7 was never marketed, and eventually Sun formed a company called FirstPerson to develop the *7 in TV set-top boxes for interactive television. Due to a variety of circumstances, the promise of interactive TV soon dried up, and Oak was left without a market. However, about the time that FirstPerson and Oak were failing, the World Wide Web was exploding. Soon companies such as Netscape began to make software that would catapult the WWW into the Internet spotlight. Sun soon realized that Oak had possibilities with the Web, and soon Oak was released to the Internet with a new name: Java.

Now Java is in its first release as a development environment, and already it is beginning to influence the direction of computing and the Internet. The Java programming language is being released free on the Internet (see Figure 1.2), and Sun is licensing the full implementation of Java and its components to a variety of Internet software vendors in the hopes of creating a new Web programming standard.

Figure 1.2. The Sun Microsystems Java Web site (

To understand a Java a little better, it is quite useful to have a better understanding of some common aspects of programming environments and practices. This book doesn't go into specifics about writing Java code until much later, but it is important to understand the basics of object-oriented programming and how it has influenced Java's development. The following sections provide an overview to some of these programming issues to prepare you for the chapters ahead.

Distributed Computing

Java is revolutionary for several reasons, but one of the most important is the way Java changes the way we use our computers. Java is designed around the notion of networks and connectivity. In today's computing world, you run applications from the hard drive in your computer. You install applications, use them, and upgrade them when they become out of date. If you need to contact someone else with your machine, you launch an application, such as a Web browser or e-mail software, that contacts a network. But imagine instead that your applications exist on a network server and are downloaded to your machine each time you use them. In this model, your software would always be current, and your machine would constantly be in touch with the network (see Figure 1.3). Distributed computing is sharing program resources across networks.

Figure 1.3. Models of distributed computing.

The end goal for Java is to be able to produce full-blown network applications. Current bandwidth and network restrictions, however, make that reality a distant goal. Instead, Java is being used to add functionality and fun to network applications such as Web browsers. With Java, you can write mini-applications, called applets, that can be incorporated into Web sites and run off a home page. For example, the tickertape applet shown in Figure 1.4 scrolls text across the screen of a Web page.

Figure 1.4. A scrolling text applet allows you to present information in an eye-catching way.

The core of distributed applications and applets is network communication. Java is designed to be integrated with networks like the Internet. Because Java has been designed to be used with the Internet and other large-scale networks, it includes a large number of networking libraries to utilize TCP/IP networking and a number of protocols commonly used on the Internet, such as HTTP and FTP. This feature means that you can easily integrate network services into your applications. The end result is a wide range of functionality on the network, and applications that lend themselves to network usage.

The idea of an applet also fits well with a distributed software model. An applet is essentially a scaled-down application. Application executables can be quite large, and often applications have a series of support files and settings files that they require in order to run. All of these files lead to large programs that are cumbersome to distribute over a network. Applet files are small and portable, which allows applets to be downloaded quickly and integrated easily into a Web page design. Applets can be added to pages to add new levels of functionality, interactivity, or just some spicy graphics and sound to make a Web site more interesting. Because applets are generally small, they don't increase the downloading time of a page significantly, and because applets are actual programs, they allow you more flexibility than traditional HTML and CGI scripts.

Security Issues and Java

Java includes security features to reinforce its use on the Internet. One potential security problem concerns the Java applets, which are executable code that runs on your local machine. Java uses code verification and limited file system access to make sure that the code won't damage anything on your local machine.

When an applet is downloaded to your local machine, the code is checked to make sure that the applet only uses valid system calls and methods. If the applet passes verification, it is allowed to run, but applets are not given access to the local machine's file system. Limiting access to the file system imposes some limits on applets, but it also prevents applets from intentionally removing files, planting viruses, or otherwise reeking havoc with your system.

Programming for the Rest of Us

Java is largely based on the C++ programming language. C++ is one of the most popular programming languages for a variety of platforms and is a high-level, object-oriented programming language. Unfortunately, C++ is a very difficult language. It is designed for high-level commercial applications; as a result, it is very powerful and gives the programmer access to a very detailed level of machine control to optimize performance. The level of control that C++ provides is often unnecessary for midrange applications and can be very difficult for an inexperienced programmer to manage.

For example, C++ gives the programmer direct control over memory management for an application. This capability means that the programmer has to be very careful about allocating new memory to an application, establishing how the application uses that memory, and cleaning out unused memory when an application is finished. Memory allocation and "garbage collection" often result in very time-consuming and complicated errors in C++ programs. Fortunately, Java handles the details of memory allocation for you, which saves you many hours of complicated debugging.

This automatic memory handling is quite a blessing. Ask anyone who has programmed in C++ what the most difficult aspect of the language is and they will answer, "pointers." Pointers are the method C++ uses to keep track of memory locations and what is stored at those locations. Imagine having to always keep track of what data went where and how to recall that data. Sound daunting? Well, it can be. In order to make programming easier, Java is explicitly designed to eliminate pointers.

Objects, Objects, Objects

Object-oriented programming has steadily been creeping into the programming world for a number of years. The concept is not that hard to grasp—objects are small pieces of code designed to perform a specific function. A programmer can then combine these objects to create a finished program.

For example, a simple program might contain an input object, a processing object, and an output object. In a calculator applet, for example, the keypad could be thought of as the input object. It receives input from the user, and then passes it on to the processor object, which does the actual calculating. The processor object then passes the result to an output object that displays the result for the user (see Figure 1.5). Objects are designed to make programming more flexible.

Figure 1.5. A breakdown of a calculator applet into object components.

Although the basic concept of object-oriented programming is simple, implementing it can be a very complex task. It involves taking a great deal of time in the planning stage of developing an applet to ensure that components are designed for portability and to work together. Well-designed objects can make programming easier in the long run, but they require considerable investment in the original planning and development. However, OOP does offer hope for the future as Java grows.

As Java begins to gain in popularity, many objects will become available as shareware and for sale as components to be used in your own applications. Someone might create a dial component that allows you to set a value with a dial, like a volume knob. The dial would need to translate its position into a value that a program would use. If this object were properly designed and documented, you could integrate it into your own applet without the headache of developing the dial from scratch; all you would need to do would be to grab the value information from the dial object. This level of program-independent functionality is the end goal and major advantage of object-oriented programming.

So now you know the truth: Java is programming. But just like any other programming language, Java can be learned with the proper documentation and time. Fortunately, Java is designed to include the functions of a high-level programming language while eliminating some of the more difficult aspects of coding in C++. Keep in mind that Java is a new language that is based on some of the more popular programming languages of today such as C and C++. Java takes its functionality from these languages, but tries to improve on their implementation whenever possible.

Java objects are also designed to make programming easier. Everything in Java is based on the object model. Even your applets are objects that can be used in other applets. This functionality was deliberately built in Java to help speed up Java development in the long run.

The earlier calculator applet example had a keypad object. Suppose you were programming an applet that looked like a phone. Phones have keypads very similar to calculators. In fact, you could pull the keypad object out of the calculator applet and plug it into your phone applet. With only some minor modifications, you could rearrange the keys to look like a phone. This way, you can take an object that has already been developed and customize it for your applications. Objects can save an enormous amount of programming time and help you create applets even faster by enabling you to reuse components.

Java Works for You

The result of all these enhancements to Java is that Java is a programming language that works for you. However, there are some trade-offs:

But even with these trade-offs, Java can be a wonderful tool. Remember, Java was developed from the ground up to fix some of the problems in higher-level programming languages. The developers of Java wanted to make it as accessible as possible and fun to learn and use. Let Java and its enhancements change the way you think about programming. In the end, you will find that Java can be fun and flexible.

A Platform-Independent Solution

Java has been molded to fit many different projects since it was first created in the early 1990s. It has gone from a programming language for personal digital assistants to a programming language or interactive TV set-top boxes, and then to its final incarnation as a Web programming language. This transformation should give you some indication of Java's flexibility and portability; it is designed to be machine-independent and function within different operating systems. Portability is one of Java's principal goals. Java code is designed to be platform-independent and has several features designed to help it achieve that goal.

When you write a Java application, you are writing a program that is designed to be run on a very special computer: the Java Virtual Machine. The Java Virtual Machine is the first step towards a platform-independent solution. When you are developing software in a language like C++, you generally program for a specific platform, such as a Windows machine or a Macintosh. The programming language will use a variety of functions that are very specific to whatever processor is used by the machine you are programming for. Because the language uses machine-specific instructions, you have to modify your program for a new processor if you want to run your program on another machine. This task can be very time-consuming and resource-intensive.

Java code is not written for any type of physical computer. Instead, it is written for a special computer called the Virtual Machine, which is really another piece of software. The Virtual Machine then interprets and runs the Java program you have written. The Virtual Machine is programmed for specific machines, so there is a Windows 95 Virtual Machine, a Sun Virtual Machine, and so on. There is even a copy of the Virtual Machine built into Netscape, allowing the browser to run Java programs.

By porting the Virtual Machine from platform to platform, instead of the Java programs themselves, any Java program can be used on any machine running a version of the Virtual Machine. This feature is the reason why the same Java applet can run on UNIX workstations as well as on Windows machines. Sun has gone to great lengths to port the Virtual Machine to nearly every major type of machine on the market. By doing so, Sun ensures the portability of Java. The benefit for you is the ability to write your code once and then use it on many machines.

One Binary Fits All

The Virtual Machine requires special binary code to run Java programs. This code, called bytecode, does not contain any platform-specific instructions. That means if you write a program on a Sun workstation and compile it, the compiler will generate the same bytecode as a machine running Windows 95. This code is the second step towards a platform-independent development environment. Imagine the time and money that could be saved if software today could be written once and used on all computers. There would be no need for Mac or Windows versions of software and no need to hire hordes of programmers to convert some Mac program to a Windows version.

When you compile a Java program, the compiler generates the platform-independent bytecode. Any Java Virtual Machine can then run that bytecode. It can be viewed using the Java appletviewer or Netscape. It can be viewed on a Sun workstation or a Windows NT machine. The bottom line is portability. One binary fits all.

Consistent Look and Feel

The Virtual Machine also allows Java to maintain a platform consistent look-and feel. When you run a Java application on a Windows machine, the menus and toolbars in your Java application look like standard Windows components. If you were to run that same applet on a Sun system, the buttons, menus, and so on would have the look and feel of the Sun system.

Because the Virtual Machine is platform-specific, the elements used by applets retain some of the host machine's characteristics. The Abstract Windows Toolkit, the standard Java interface builder, accesses windowing and interface components from the host computer. This feature allows you to create easy-to-use interfaces without much specific programming. If the person using your applet is a Mac user, he can use the applet just like he uses other Mac applications. If she is a Windows user, your applet appears to her as a normal Windows application. This feature helps avoid user confusion by allowing users to work in a familiar environment without having to learn any new cumbersome user interfaces.

Interactive Web Pages

When the Web began, it was simply a means of sharing static data. Text or images were downloaded and displayed by a browser. There wasn't any interaction, and the pages didn't change unless you reloaded a new version. Eventually, with forms and CGI scripts, the Web became more interactive, but it still lacked any aspects of multimedia or real-time interaction. With Java, you can implement animation (see Figure 1.6), sound, real-time input, and data manipulation. It is even possible to use applets in conjunction with other applications, such as databases, to add new levels of functionality to Web sites.

Figure 1.6. A simulation of Bay Area Rapid Transit Trains running on schedule, developed at Lawrence Berkeley Labs, The map shows trains running on various routes around the city.

When Java was first introduced, many of the first applets showcased Java's ability to offer animation to the Web. Even today, animations tend to dominate many Java applets (see Figure 1.7). Because Java applets can easily flip through standard format images (GIF and JPEG), it is natural that this use would flourish. Previous attempts at animations on the Web had relied on network- and server-intensive methods for displaying one image at a time or server push animation. The result was slow, clunky, and often even nonfunctional animations. Because Java applets run on your local machine, they don't tax the network while they are running, and the animations are smooth.

Figure 1.7. A Java-animated marquee for an on-line art show. The animation draws attention and highlights the nature of the site.

You can use animations and other types of image-based applets for more than just spicing up a Web page. Many sites are demonstrating how Java's animation functionality can be put to productive use ( see Figure 1.8). Experimental Java sites are showing how applets can aid scientific, as well as artistic, visualization.

Figure 1.8. The NASA SeaWIFS site. This site showcases a NASA project designed to monitor the Earth's oceans. Dynamically updating data is one way applets can add to a Web page.

You can add any sort of animation or informational graphic to a page. Many sites are using Java applets to enhance content in an informational and entertaining way (see Figure 1.9).

Figure 1.9. The Hotwired on-line magazine site has begun incorporating Java applets to enhance on-line articles. These applets can add a new dimension to the Web articles that distinguish them from their print counterparts.

The interactive possibilities provided by Java go to the other extreme of user interactivity as well. Keep in mind that Java applets can utilize limited network connectivity. That means it is possible to join applets, and the users of applets, over the Internet. A good example of this capability is Gamelan's interactive chat (see Figure 1.10). The interactive chat featured on the Gamelan Web site ( is an example of how functional Java can be. This applet allows users to sign on to a chat session and write messages to each other in real time. Similar network applications have been around for some time now, but imagine adding this capability to a Web page! This applet is so flexible that it can even be converted to a standalone window, separating it from the Web page altogether.

Figure 1.10. The Gamelan Chat applet in float mode, which allows the applet to occupy a separate window.

This level of interactivity was really unheard of on the Web until Java came along. Now you can imagine the types of applets that are possible, such as real-time stock quotes, interactive chats, shopping areas, and catalogs.


You've chosen a very exciting time to begin exploring Java. The World Wide Web is growing by leaps and bounds every day, and Java is helping to shape its development. You've seen some examples of what Java can do for the Web, and how it offers a promise of interactivity on the Web that was not previously available. You also have a glimpse at the programming realities that make Java possible.

The coming chapters discuss the progression of Java in more detail. They tell where to get access to Java and Java applets and discuss Java-ready tools. They also cover some of the design issues that are raised by Java and talk about adding Java to your own pages. Finally, you'll learn how to create your own applets. In the end, you should come away with a comprehensive look at Java from a user and a creator's standpoint and feel confident enough to use Java to enhance your experience with the World Wide Web.

Previous Page TOC Next Page