«^»
4.2. Getting Java bytecodes executed when a WWW page is visited

Since the inception of the WWW in the early 1990s, people have been finding different ways of making a WWW page more appealing to the visitor to the page. When Sun first produced Java in 1995, they thought it would be useful if Java code could be executed as part of browsing a WWW page. The Java code could do some processing and display its output within the pane of the WWW browser. They showed that this was possible by producing a WWW browser that had this capability - was first called WebRunner and later called HotJava. They then persuaded Netscape whose browser (Navigator) was the most popular at that time to include a Java interpreter as part of the code of Navigator. Support for Java within Microsoft's Internet Explorer came later.

In order that the author of a WWW page could indicate which Java .class file was to be executed when the WWW page was loaded, HTML was altered to include an APPLET tag. Here is an example of some HTML that includes an APPLET tag. Suppose that this text is stored in the file HelloApplet.html.

0259: <HTML>
0260: <HEAD>
0261: <TITLE>The HelloApplet Example</TITLE>
0262: </HEAD>
0263: <BODY>
0264: <P>
0265: Start.
0266: </P>
0267: <APPLET CODE="HelloApplet.class" WIDTH="150" HEIGHT="25">
0268: <P>Java does not seem to be supported by your WWW browser</P>
0269: </APPLET>
0270: <P>
0271: Finish.
0272: </P>
0273: </BODY>
0274: </HTML>

WWW browsers ignore tags that they do not understand. So if a WWW browser is given this HTML and it does not understand the APPLET tag, it will display the message Java does not seem to be supported by your WWW browser. However, if a WWW browser is capable of running Java, then, when the HTML interpreter of the browser sees this APPLET tag, it will start to obtain the bytecodes from the file mentioned in the CODE attribute of the APPLET tag. So, with the HTML given in the file HelloApplet.html, it would download the bytecodes that are in the file HelloApplet.class. Unless you also include a CODEBASE attribute, the browser will assume that this file is in the same directory from which it is obtaining the file containing the HTML instructions.

These bytecodes will be transferred from the .class file into a storage area known to the Java interpreter of the WWW browser. Often the bytecodes of a .class file will take some time to be transferred and so the rest of the WWW page is likely to be displayed before they arrive. When the bytecodes have finally arrived, the browser's Java interpreter will execute them.

So, although the author of the WWW page compiled the Java source code on his/her computer, the .class file(s) that were produced by the compiler will be executed by the Java interpreter contained in a WWW browser that is running on the computer of the person visiting the WWW page.