webservice中的session的管理

本文介绍了WebService应用中Session管理的实现方式,包括Session接口定义和简单实现类SimpleSession的具体代码,并提供了SessionID随机生成的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值