You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public void writeObject(@NonNull ObjectOutputStream stream) throws IOException {
stream.writeObject(id);
stream.writeLong(createdTime);
stream.writeLong(lastAccessedTime);
stream.writeInt(maxInactiveInterval);
stream.writeBoolean(isNew);
stream.writeBoolean(isValid);
stream.writeInt(mAttributes.size());
String keys[] = mAttributes.keySet().toArray(EMPTY_ARRAY);
for (String key: keys) {
Object value = mAttributes.get(key);
if (value != null && value instanceof Serializable) {
stream.writeObject(key);
stream.writeObject(value);
}
}
}
writeObject方法仅在replace方法中有调用,
replace方法在add方法中调用
@Override
public boolean replace(@NonNull StandardSession session) throws IOException {
Assert.notNull(session, "The session can not be null.");
String id = session.getId();
if (TextUtils.isEmpty(id)) {
throw new IllegalStateException("The session id can not be empty or null.");
}
ObjectOutputStream writer = null;
try {
if (!IOUtils.createFolder(mDirectory)) {
return false;
}
File file = new File(mDirectory, id);
if (!IOUtils.createNewFile(file)) {
return false;
}
writer = new ObjectOutputStream(new FileOutputStream(file));
session.writeObject(writer);
return true;
} catch (IOException e) {
IOUtils.delFileOrFolder(new File(mDirectory, id));
throw e;
} finally {
IOUtils.closeQuietly(writer);
}
}
发现这个方法只能在req.getValidSession()获得新session对象后在本次request中调用才能存储,一次HTTP请求-响应结束后,后面再获取这个session再存储attribute会存储不了,因为没有序列号到文件上。
attribute存储在mAttributes中,通过writeObject方法序列化到文件中,后面再通过readObject反序列成为对象
writeObject方法仅在replace方法中有调用,
replace方法在add方法中调用
需要满足 session.isNew(),isNew属性仅在newSession()方法中被赋值为true(反序列化时可以忽略,不会改变值)。
后被赋值为false,自此这个session的isNew属性一直为false,再也没有机会重新序列化到文件中,也存储不了attribute
newSession()方法仅在getValidSession()时会调用。也就是仅在getValidSession()调用后,才会存储到文件中,
The text was updated successfully, but these errors were encountered: