Session.java代码如下:
import java.util.Enumeration;
public interface Session{
public Object get(String key);
public void set(String key, Object value);
public void remove(String key);
public Enumeration getKeys();
public void setTimeout(int timeout);
public int getTimeout();
public void touch();
public void invalidate();
public Object getLockObject();
}
SimpleSession.java代码如下:
import java.util.Enumeration;
import java.util.Hashtable;
public class SimpleSession implements Session {
private Hashtable rep = null;
private int timeout = -1;
private long lastTouched;
public SimpleSession(){
lastTouched = System.currentTimeMillis();
}
public Object get(String key){
if (rep == null){
return null;
}
lastTouched = System.currentTimeMillis();
return rep.get(key);
}
public void set(String key, Object value){
synchronized (this) {
if (rep == null){
rep = new Hashtable();
}
}
lastTouched = System.currentTimeMillis();
rep.put(key, value);
}
public void remove(String key){
if (rep != null){
rep.remove(key);
}
lastTouched = System.currentTimeMillis();
}
public Enumeration getKeys() {
if (rep != null){
return rep.keys();
}
return null;
}
public void setTimeout(int timeout){
this.timeout = timeout;
}
public int getTimeout(){
return timeout;
}
public void touch() {
lastTouched = System.currentTimeMillis();
}
public void invalidate() {
rep = null;
lastTouched = System.currentTimeMillis();
timeout = -1;
}
public long getLastAccessTime(){
return lastTouched;
}
public synchronized Object getLockObject() {
if (rep == null) {
rep = new Hashtable();
}
return rep;
}
}
【注】:上述代码就是在WebService的应用中的Session管理代码。
--------------------------------------------------------------------------------------------------
随机生成一个SessionID的值:
SessionUtils.java代码如下:
import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;
import java.util.Random;
public class SessionUtils {
protected static Log log = LogFactory.getLog(SessionUtils.class.getName());
protected static final int SESSION_ID_BYTES = 16;
protected static Random random = null;
protected static String randomClass = "java.security.SecureRandom";
private static String thisHost = null;
public static synchronized String generateSessionId() {
byte bytes[] = new byte[SESSION_ID_BYTES];
getRandom().nextBytes(bytes);
StringBuffer result = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
byte b1 = (byte) ((bytes[i] & 0xf0) >> 4);
byte b2 = (byte) (bytes[i] & 0x0f);
if (b1 < 10) {
result.append((char) ('0' + b1));
} else {
result.append((char) ('A' + (b1 - 10)));
}
if (b2 < 10) {
result.append((char) ('0' + b2));
} else {
result.append((char) ('A' + (b2 - 10)));
}
}
return (result.toString());
}
public static synchronized Long generateSession() {
return new Long(getRandom().nextLong());
}
private static synchronized Random getRandom() {
if (random == null) {
try {
Class clazz = Class.forName(randomClass);
random = (Random) clazz.newInstance();
} catch (Exception e) {
random = new java.util.Random();
}
}
return (random);
}
}
转载自:http://blog.sina.com.cn/s/blog_59754a020100ws7x.html