Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 21: Use archive files to speed up applet loading

Crank up the perceived performance of your applet by making it load faster

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
One of the ongoing complaints about Java is its performance. A big factor in the user's perception of the speed and value of Java applets is the time it takes to download all of the classes that make up the applet. We have all had the pleasure of waiting a minute or more for an applet to download just to see some silly animated graphic. Unfortunately, this negative performance perception can spill over to applets that are actually useful.

To understand why the new applet loading method is fast, you need to know why the current method is so slow. A given applet usually is composed of a number of Java .class files. For each of those class files, the class loader must open an individual socket connection from your browser to the server that the applet code resides on. So, if your applet is composed of 18 .class files, the browser must open at least 18 sockets so it can transfer each one of those files. The overhead to set up and tear down each of those connections is quite significant. For example, each connection set up requires a number of network packet round-trips, which greatly increases the overall latency (especially in these times of the increasingly congested 'Net). (For the nitty-gritty details of socket connection overhead, check out one of the weighty TCP/IP reference tomes.)

By this point you have already figured out the solution to this problem: Put all of the .class files into one big file so only one connection must be made to download everything. Good thinking! That's exactly what the folks at the two big Java browser camps (Netscape and Microsoft) thought of.

Unfortunately, the two solutions that they came up with are not directly compatible. Microsoft, in its need to be different, created its own CAB file format. The Netscape solution is to use the existing, well-known .zip archive file format. Luckily, we can write our HTML code to handle both formats if we want to. This is because each of those special file formats is specified by a separate extension to the &ltAPPLET> HTML tag.

I'm not going to speak to the creation of CAB files (since they should be going away). For those who are really interested, check out the Microsoft Java developer documentation. Once you have created a CAB archive you can use it by adding a cabbase HTML parameter to the <APPLET> tag:

&ltapplet name="Hello" code="HelloWorld" width="50" height="50">
   &ltparam name=codebase value="http://www.foo.com/classes">
   &ltparam name=cabbase value="hello.cab">
</applet>


The value of the cabbase parameter is the name of the CAB file.

Creating a .zip file archive that can be used with the Netscape browser is easy. Zip up all of the .class files that are needed for your applet into a single .zip file. The only thing to remember is that you must only store the files into the archive (that is, no compression)!

If you are using PKZip:

pkzip -e0 fileArchive.zip listOfClassFiles


If you are using the Info-Zip Zip program:

zip -0 fileArchive.zip listOfClassFiles


Note that in both cases, the command-line flag contains a zero rather than a letter "O";

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

applet do not work with jet planBy Anonymous on June 17, 2009, 4:05 amapplet do not work with jet plan

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources