這個是tomcat6.x的原始碼,
org.apache.catalina.session.StandardSession
使用如下的結構來儲存session的值
protected Map attributes = new ConcurrentHashMap();
在
public void setAttribute(String name, Object value)
裡,實際調用了
setAttribute(name,value,true);
在
public void setAttribute(String name, Object value, boolean notify)
裡,如果value是0,則調用removeAttrbute方法,將屬性移除
if (value == null) {
removeAttribute(name);
return;
}
否則調用
Object unbound = attributes.put(name, value);
將數值儲存到裡面,同時拿到以前的值
我們來看看
public void removeAttribute(String name, boolean notify)
調用了
protected void removeAttributeInternal(String name, boolean notify)
// Avoid NPE = NullPointerException 呵呵,就是null 指標
if (name == null) return;
// Remove this attribute from our collection
// 就是簡單的從Map裡面刪除
Object value = attributes.remove(name);
// 後面還有一些事件通知的方法調用
可見在Session裡,我們常用的操作就是這麼實現的,沒有什麼高深的東西。
只要我們能拿到屬於我們的session對象,就可以拿到我們儲存在裡面的東西了。