In previous blog entry, I had just mentioned about our server side context sharing mechanism between an applet and an web pages in our GIS enabled project. Users will try to accomplish their scenarios by using those two views. Obviously there will be switches many times between them during execution of any use case. Simply, users will enter some data via web interface and then switch to the applet view and enter some further data in there and then switch back to the web interface and submit data. Later they will switch to applet view again to see the results of that submit action.
There is one big problem here. As web interface part and applet view part will be in different web pages, IE calls destroy method of applet instance each time when we leave the page in which applet resides, an calls init each time when we enter into that page again. IE behaves like this between page back and forward operations, too.According to specified operations above, the life cycle of an applet in IE is as follows:
init() -> start() : when we enter the page, in which our applet is contained stop() -> destory() : when we leave applet page, or click page back/forward buttons init() -> start() : when we again come back to the applet's web page
As a result of the above behaviour, our GIS applet gets initialized several times during execution of a user scenario, which is unacceptable, because users may enter some data, open several maps during their operations and may also switch back and forward between applet and web interface part.
I tried to find an elegant solution to this multiple init -> destroy problem. I looked for a way to prevent browser from calling those methods but came without a solution. Through a deep googling around the web, I found relatively old discussions (dating back to year 2000 in comp.lang.java.programmer) related with our problem. I think the only possible solution to this problem is caching state, data or graphics, which you don’t want to get destroyed and again initialized in a static data strutcture in your applet code. In init() method, or during other phases of applet, you first look at the cache before you create your graphics, or load user data, and only create or initialize them unless they exist in the static cache. This cache could simply be a Hashtable. The trick is here that loaded applet class definition and, therefore static variable is kept alive until all open browser windows are closed.