Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

bootstrapping big applet via https

We have a very big applet on an https site. The task is to write a small applet which starts at once and tells the user to wait for the big applet and disappears when the big applet loaded.

My solution is to use two applets on the same page, the small one first, then the big one. The small one watches till the second one is loaded, and this worked very fine. Then suddenly this doesn't work any more: Both applets are waiting till the big one is loaded.

Does someone know about how several applets on the same page are initialized?
0
dnoelpp
Asked:
dnoelpp
  • 9
  • 9
1 Solution
 
mohan_sekarCommented:
Hi,

     Try the following code. I hope this would help you


import java.net.*;  // URL
import java.awt.*;  // Image, MediaTracker
import java.applet.Applet;

/** A simple class that loads an image over the net, waiting until
 * the image is loaded until continuing. Also lets you pass a string


 * <P>
 * Example:
 * <PRE>
 *   import ImageWait;
 *<P>
 *   public class Foo extends Applet {
 *     Image duke
 *<P>
 *     public void init () {
 *      ImageWait iw = new ImageWait(this);
 *      duke = iw.getImage("http://java.sun.com/lib/images/duke.gif");
 *      }
 *<P>
 *      public void paint(Graphics g) {
 *        g.drawImage(duke);
 *      }
 *   }
 * </PRE>

public class ImageWait {

  // The Applet on which the drawImage will be performed.
  Applet applet;
 
  //--------------------------------------------------------------

  /** Sets the applet to be associated with the MediaTracker,
   *  (which does the checking to see if the image is done loading).
   *
   * @param applet The applet in which the image will be drawn.
   */
   
  public ImageWait(Applet applet) {
    this.applet = applet;
  }
 
  //--------------------------------------------------------------

  private Image waitForLoading(Image image, String urlString) {
    MediaTracker tracker = new MediaTracker(applet);
    tracker.addImage(image, 0);
    try {
      tracker.waitForID(0);
    } catch (InterruptedException i) {
    } catch (Exception e) {    
      System.out.println("[ImageWait.waitForLoading], exception " +
                e.getMessage());
      e.printStackTrace();
    }
    if (tracker.isErrorID(0)) {
      System.out.println("Error loading image " + urlString);
      return(null);
    }
    return(image);
  }
 
  //--------------------------------------------------------------

  /** Loads the specified image, <B>waiting until the image is done
   *  loading before continuing.</B> Passing a string lets you
   *  skip embedding the try/catch in the new URL... call in the Applet.
   *
   * @param urlString A String representing the URL of the image.
   * @return The image specified (or null if URL can't be found).
   */
  public Image getImage(String urlString) {
    URL url = null;
    Image image = null;
   
    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      System.out.println("Malformed URL: " + urlString);
      return(null);
    }
    image = applet.getImage(url);
    return(waitForLoading(image, urlString));
  }

  /** Loads the specified image, <B>waiting until the image is done
   *  loading before continuing.</B>
   *
   * @param url A URL object for the image.
   * @return The image specified (or null if URL can't be found).
   */
  public Image getImage(URL url) {
    Image image = applet.getImage(url);
    return(waitForLoading(image, url.toExternalForm()));
  }
 
  /** Loads the specified image, <B>waiting until the image is done
   *  loading before continuing.</B>
   *
   * @param url A URL object for the directory of the image.
   * @param relativePathname A String of the filename of the image in
   *        the directory.
   * @return The image specified (or null if URL can't be found).
   */
  public Image getImage(URL url, String relativePathname) {
    Image image = applet.getImage(url, relativePathname);
    return(waitForLoading(image, url.toExternalForm() + relativePathname));
  }

  //--------------------------------------------------------------
}

:-)

bye

Mohan
0
 
dnoelppAuthor Commented:
No, that's not a solution! I am not waiting for an image being loaded. Please read my question more carefully. Thanks.
0
 
omry_yCommented:
did you suddenly put your applets in a cab or jar?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dnoelppAuthor Commented:
Yes, but is this really the cause? Because the waiting applet is in a different .jar file than the big applet.
0
 
dnoelppAuthor Commented:
Anyway, I have found this solution:

Replace the first applet with JavaScript animation. This animation is stopped when it discovers the applet got loaded. How to discover it? It watches the status bar. Hahahaha! That simple! :-)

Or am I mistaken? Please give your comment, there are still some points to be given for you!
0
 
omry_yCommented:
yes, the cause is that you put your applets in a cab file. the browser wait for the cab(s) to download, and than starts the applets.

how big is your applet?


your java script can 'discover' that the applet is loaded by checking the applet isReady() method.


0
 
omry_yCommented:
oops.
the method is applet.isActive(), not applet.isReady().
0
 
dnoelppAuthor Commented:
Yes, it could be... Even if both applets have different .jar files. The small .jar file is about 2 kB big, the big one I still don't know, but about 1 MB. The problem is that the applet isn't just used in an intranet (that's why https), but it will be used by a very small clientele outside the customer's company. They will be told that they have to wait for about ten minutes first time, and then later perhaps only one minute. The applets are Swing applets, that's an additional twist because of the installation need of the JRE.

A possibility would have been to bundle JSSE into the small applet and use an URLClassloader to bootstrap the big applet. But the applet is used in a context of an authorizing manager which works with cookies. Without these cookies no file can be retrieved by the https connection...

And isReady() is cross-browser?
0
 
omry_yCommented:
1.you depend on the browser cache for it?
its not really trust worthy..

isActive (not isReady) is a method in java.applet.Applet, so its cross browser.
an applet is ready after the init (or start, I dont remember exactly) is called.
0
 
dnoelppAuthor Commented:
Okay, isActive() is an java.applet.Applet method. So no help to call it from Javascript. :-( I know, Netscape makes it possible to call applet methods from JavaScript, but Internet Explorer...?

As for the browser cache the idea is that the user get the feeling the computer is still working while it is loading the big applet. So, if the big applet is already in the cache, it doesn't matter what the small applet is doing, because the big applet started up very soon.
0
 
omry_yCommented:
this is how you call an applet in explorer, it should also work for netscape, but I am not sure.

document.applets("AppletName").isActive()


say.. how much is your company willing to pay for a solution that will cache the applet in the user hard disk permenently, show the user a download progress report while updating, and that will allow you to update the applet for the user at a granularity of a single file (class file, gif, whatever)?
I am asking becasue I have developed such a solution for my company, and maybe, just maybe, we will be interested in selling it.

if you want to have a look, you can see it at
http://212.150.35.81/mapapplet/index.html

to un-install, go to explorer java home directory, (probably in c:\winnt\java or c:\windows\java)
0
 
omry_yCommented:
and delete the com directory.
0
 
dnoelppAuthor Commented:
The problem is https. Say, your applet is using an URL instance to bootstrap other applets, isn't it. This doesn't work for us. We can only load data via the browser. (for example with showDocument()).
0
 
omry_yCommented:
why use https in the first place?
can`t you put the applet on a regular http server?
0
 
dnoelppAuthor Commented:
This is a project requirement. Everything what belongs to the project is homed within an authorization manager. Each request is done within https and some cookies set by the authorization manager. To use URL we would need to integrate part of JSSE into the bootstrapper and secondly retrieve the cookies (they could passed in as parameters by the JSP page hosting the applet) and thirdly pass them to the URL. This is too much of an effort just for a small bootstrapper. Keep in mind, we have a limited clientele and they can be instructed very well not to run away when the applet loads. Thanks for your offer.
0
 
omry_yCommented:
okay.
so, did the isActive thing worked?
0
 
dnoelppAuthor Commented:
I didn't try it out. Since watching the status bar effectively worked... It's on my todo list to experiment with it later, because this will mean that JavaScript code can call applet methods... I think, this doesn't work with IE.
0
 
omry_yCommented:
it does work with IE for sure.
(tested)
0
 
dnoelppAuthor Commented:
Thanks for the comments.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now