CounterBeanpublic class CounterBean extends Object implements SerializableThis class maintains page counters.
Note that this class is intended to be used in session and application
scope so regular setter and getter methods for pageContext can not be
used; all info needed to get and increment the counter value must be
provided as parameters to the corresponding
method to avoid multi-threading issues. |
Fields Summary |
---|
private Hashtable | counters |
Methods Summary |
---|
public int | getCurrentValue(java.lang.String uri)Returns the current value of the counter for this page.
int value = -1;
int[] counter = (int[]) counters.get(uri);
if (counter != null) {
value = counter[0];
}
return value;
| public int | getNextValue(java.lang.String uri)Increments the counter for this page and returns the new value.
incrementValue(uri);
return getCurrentValue(uri);
| public void | incrementValue(java.lang.String uri)Increments the counter for this page. If the permanent is true,
the new value is also saved in a file.
/*
* Synchronized block for incrementing the counter to handle
* concurrent calls from multiple threads
*/
synchronized (counters) {
int[] counter = (int[]) counters.get(uri);
if (counter == null) {
counter = new int[1];
counter[0] = 0;
}
counter[0]++;
counters.put(uri, counter);
}
|
|