PRB: Sharing Static Variables Across Multiple Applets (234364)
The information in this article applies to:
- Microsoft virtual machine
- Microsoft Visual J++ 6.0
This article was previously published under Q234364 SYMPTOMS
When you access a static variable in one class from other classes, there seem to be multiple instances of the static variable.
RESOLUTION
To share static variables across multiple classes, each class must be loaded by a common classloader. Classes loaded by different classloaders will each have their own copy of the static variables.
To ensure that the classes are loaded via a common classloader, make sure that the following conditions are met:
- Classes must share the same codebase URL.
- The pages that host the classes must come from the same security zone.
- The same values must be specified in the ARCHIVE, CABBASE, NAMESPACE, and CABINETS tags for each class if those tags are used.
NOTE: Values are compared using a case-sensitive string-literal comparison, so the strings must match exactly.
STATUS
This behavior is by design.
MORE INFORMATIONSteps to Reproduce Behavior- Compile the following four Java files into classes.
- Place the class files from the first two java files into a .zip file named AppletChatServer.zip and place the other two class files plus AppletChatClient$1.class into AppletChatClient1.zip and AppletChatClient2.zip, respectively.
- Move these .zip files into a subfolder named Classes.
- View each of the HTML samples in turn.
AppletChatServer.java
import java.applet.*;
import java.util.*;
public final class AppletChatServer {
public static Vector v=new Vector();
public static void register(AppletChatClient client){
if(!v.contains(client)) v.addElement(client);
}
public static void broadcast(String msg){
for(Enumeration en=v.elements();en.hasMoreElements();)
((AppletChatClient)en.nextElement()).receive(msg);
}
}
AppletChatClient.java
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class AppletChatClient extends Applet {
TextArea ta=new TextArea();
Button bu=new Button("Send");
Panel pa=new Panel();
TextField tf=new TextField("Type your message here...");
public void init(){
AppletChatServer.register(this);
setLayout(new BorderLayout());
add("South" ,bu);
add("Center",pa);
pa.setLayout(new BorderLayout());
pa.add("South",tf);
pa.add("Center",ta);
bu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
AppletChatServer.broadcast(tf.getText());
tf.selectAll();
}});
}
public void receive(String msg){
ta.append(msg+"\n");
}
}
AppletChatClient1.java
public class AppletChatClient1 extends AppletChatClient {
public AppletChatClient1() {
super();
}
}
AppletChatClient2.java
public class AppletChatClient2 extends AppletChatClient {
public AppletChatClient2() {
super();
}
}
In the first of the two following HTML samples, the unpaired applets don't interact with each other. There are no errors, and everything seems like it will function normally, but each applet pair has it's own copy of the static variables. test1.html
<HTML>
<BODY>
<APPLET code=AppletChatClient1.class
id=Applet3
name=AppletChatClient1
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip">
</APPLET>
<APPLET code=AppletChatClient2.class
id=Applet0
name=AppletChatClient0
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient2.zip">
</APPLET>
<BR>
<APPLET code=AppletChatClient1.class
id=Applet3
name=AppletChatClient1
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip">
</APPLET>
<APPLET code=AppletChatClient2.class
id=Applet0
name=AppletChatClient0
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient2.zip">
</APPLET>
</BODY>
</HTML>
When you run this HTML sample, you'll see that everything is functioning as expected. The only change is that the ARCHIVE tags all match exactly. test2.html
<HTML>
<BODY>
<APPLET code=AppletChatClient1.class
id=Applet3
name=AppletChatClient1
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip,AppletChatClient2.zip">
</APPLET>
<APPLET code=AppletChatClient2.class
id=Applet0
name=AppletChatClient0
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip,AppletChatClient2.zip">
</APPLET>
<BR>
<APPLET code=AppletChatClient1.class
id=Applet3
name=AppletChatClient1
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip,AppletChatClient2.zip">
</APPLET>
<APPLET code=AppletChatClient2.class
id=Applet0
name=AppletChatClient0
height=200
width=320
CODEBASE="./classes"
ARCHIVE="AppletChatServer.zip,AppletChatClient1.zip,AppletChatClient2.zip">
</APPLET>
</BODY>
</HTML>
Modification Type: | Major | Last Reviewed: | 10/20/2003 |
---|
Keywords: | kbJava kbprb KB234364 |
---|
|