next up previous
Next: 4 Making The Jar Up: Web Applets in Scala Previous: 2 The Scala Code


3 The HTML File

In order for our applet to render on the web, we need to make an HTML file that loads our code. Those familiar with HTML, or other ways to make web pages, will be able to make a more complex and fancy web page, but ours will be simple for now. Open up a new text file named index.html and place the following in this file:

<applet code="MyFirstApplet.MainApplet" archive="MyFirstApplet.jar" width="500" height="600">
Sorry, this browser does not understand Java applets or they are not enabled right now.
</applet>

This simple HTML file is sufficient to display our Applet (once we compile it in section 4). There are a few things to note here. The applet tag is used to specify that we are inserting an applet here. The big thing to pay attention to here are the attributes within this tag. First, the code attribute is where you specify the main class of your applet. MyFirstApplet corresponds to the package name of our applet, which is followed by a . then the name of the main class, in our case, MainApplet. The next attribute is the name of the jar file containing the code for the applet. We have not made this jar file yet, but planning ahead, we will call it MyFirstApplet.jar. The final two attributes are width and height. You will notice that these two numbers are the same numbers we used when we decided the dimension of MainApplet's ui. This is not a coincidence. After ending the applet tag declaration, we type a message that will be displayed if the user's browser either does not support Java applets or if Java applets are not enabled. Finally, we close off the applet tag.

As I said, this is a very simple HTML page that will simply display our applet, or an error message if applets are not supported/enabled. One problem with Java web applets is due to web browser caching. Once you run the applet once, it will not redownload if you refresh the page, even if you have recompiled the jar file. We need to force the browser to redownload the jar file whenever it has been updated. In order to do this, we can change the index.html file by adding a "version" number to the jar file. I put "version" in quotes, because we do not actually have to have a version number. Instead, my solution (or hack, if you prefer) is to append a random number to the name of the jar file in the HTML file. We do not actually change the name of the jar file, though. I made a shell script that works with the Bash shell that will generate an HTML file with a random "version" number in it, so that the user's browser will redownload all of the files. The idea is that you will run this shell script to generate a new HTML file whenever you update the jar, then use this new index.html file to display the applet. The shell script is below. Feel free to change it as needed.

#!/bin/bash
echo "<applet code=\"MyFirstApplet.MainApplet\" archive=\"MyFirstApplet.jar?v=$RANDOM\" width=\"500\" height=\"600\">" > index.html
echo "Sorry, this browser does not understand applets or they are not enabled." >> index.html
echo "</applet>" >> index.html

Note: Shell scripting is beyond the scope of this tutorial, so if you do not already know what you are doing and are not willing to learn on your own, then you can simply modify the original index.html file, replacing MyFirstApplet.jar with MyFirstApplet.jar?v=num where num is any combination of numbers. You will need to modify this file each time you upload a new jar file to the web.

We now have both our Scala code and our HTML file. In the next section, we will compile our jar file with our code and the Scala libraries, then use Proguard to shrink this file down to a reasonable size.


next up previous
Next: 4 Making The Jar Up: Web Applets in Scala Previous: 2 The Scala Code
2008-08-28 2010-12-16