FormBean的Session序列化问题
异常信息: 2013-9-25 22:39:43 org.apache.catalina.session.StandardManager start 严重: Exception loading sessions from persistent storage java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.zyx.strutsdemo.user.domain.User at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.defaultReadFields(Unknown Source) at java.io.ObjectInputStream.readSerialData(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1509) at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:998) at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:395) at org.apache.catalina.session.StandardManager.load(StandardManager.java:322) at org.apache.catalina.session.StandardManager.start(StandardManager.java:649) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4714) at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3461) at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426) at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1361) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642) at java.lang.Thread.run(Unknown Source) Caused by: java.io.NotSerializableException: com.zyx.strutsdemo.user.domain.User at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1585) at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1015) at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:529) at org.apache.catalina.session.StandardManager.unload(StandardManager.java:470) at org.apache.catalina.session.StandardManager.stop(StandardManager.java:679) at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4882) at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3454) ... 7 more |
产生异常的代码: public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
解决问题后的代码:
public class User implements Serializable{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
总结:
如果要把session对象复制到硬盘上,或者集群中的其他机器或者网络中的其他机器,即把session对象序列化,那么需要实现Serializable接口。否则java提供的OutPutStream的writeObject方法会抛出 java.io.NotSerializableException。 |