每当我向会话添加可序列化的对象时,Tomcat都会给我一个新的会话,并且不会添加该对象。我可以添加字符串和整数。
当我将类定义从JSP scriptlet(用于快速和肮脏的测试)移动到其自己的类文件并删除static
修饰符时,可以将该对象存储并复制到memcached中。
为什么静态类对象会破坏会话?
context.xml:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="tomcat-session-cache.XXXXXX.cfg.XXXX.cache.amazonaws.com:11211"
memcachedProtocol="binary"
sticky="false"
sessionBackupAsync="false"
requestUriIgnorePattern=".*\.(gif|jpg|jpeg|png|ico)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
我如何将对象添加到会话中:
public static class SessionObject implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public String str;
public int num;
public SessionObject(String str, int num) {
this.str = str;
this.num = num;
}
}
...
session.setAttribute("objInSession", new SessionObject("test", 123));