This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi there, I have to build an application which is developed in applets. I have a main page from which i navigate and go all the other applets(web pages). Now, if i have to come back to the first page, will the applet be instantiated into the browser again or it will pickup the old instance of the applet in memory. Is there any way to pick up the old instance from the memory?.
Hi, If all your links to webpages open in the same window, then, chances are that the applet will be re-instantiated. But, again that depends on the different browsers and their quirks. Ideally, a browser calls the applet's destroy() method and unloads it from memory when the html page containing the applet goes out of scope. If you have state that needs to be stored, the best way is to store it on the server side using the destroy() method. When the applet loads again, check if there is state saved on the server, if yes, get the data and load the applet OR load the applet using the default data. But, this would require a server side java program running all the time like a servlet and also this could significantly increase the load time and also may not be feasible. The other alternative is to have the applet signed and request File access permission. Thus, you write the data to the local file system and when the applet is init again, get the data from the file. But, the downside to this approach is that it is expensive to sign applets. Hope this helps Ashwin.
Sriram, I believe some of what Ashwin said is wrong... If you have an applet running on a page and go to another page the destroy() method will not be called, only the stop() method... when you return to that page the start() method of the applet will be called. The only time an applet's destroy() method is called is when the browser is closed... That means that your applet is still alive in the JVM inside the browser until it is closed. So look into the start() and stop() methods of Applet... they may help you out with your problem... unless you are talking about restoring values after the browser has closed... then you would have to do as Ashwin suggested and work on the server-side. HTH, -Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Jul 17, 2000
Hi Nathan, I agree that only stop() should be called when the applet page scope is lost and destroy() when the applet is unloaded. But in reality, stop() and destroy() get called when the page scope is lost. Thus, the browser unloads the applet once it displays a new page. And, the JVM is still running at that time. The browser JVM is free to unload an applet at any time. Sure enough, an applet is unloaded when the browser is closed but, that is not the only event causing the unload. I have tested this out in IE 5.0. Again, it might be different in different browsers and hence I have written "that depends on the different browsers and their quirks" in my previous post. Ashwin.
[This message has been edited by Ashwin Desai (edited March 01, 2001).]
Joined: Mar 01, 2001
Hi Ashwin / Nate, Thanks for the help. I will go ahead and test the applets stop() and destroy() methods. My point is to really have the applet not to be loaded so many times in a same session on the browser. I only want to get to the previous state of the applet in the same session without loading it again. Will check it out. Thanks Sriram
Ashwin, Sorry, I thought that it was a standard that the JVM must keep applets in them until browser close... I have mainly used Netscape... if IE does it that is just another way M$ has managed to screw up Java once again. -Nate
Joined: Mar 01, 2001
Hi Guys, I tried out the applet's scope in NS. The applet is loaded into memory in the first instance and goes to stop() method when we switch to other applets. But if we come back to the same applet after going through various other applets, the original applet is actually destroyed and re-loaded again. I had Java console on and it gave all the methods in the sequence in which the applet is triggered. Thanks for your help, Sriram