编程中的会话复制(Weblogic 和 Java)
我想知道调用 session.setAttribute 或 session.getAttribute 的对象是否还需要实现“java.io.Serialized”以支持会话复制?
例如
User 类实现了 Serialized 接口。 但如果调用“session.setAttribute”的类SomeUtilityClass需要实现Serialized接口。
/* Does it also need to implement the Serializable here? */
public class SomeUtilityClass{
public void test(HttpServletRequest request){
request.getSession().setAttribute("user", new User());
}
}
public class User implements Serializable{
private static final long serialVersionUID = 12345432534456654664L;
//skip the getter & setter...
}
如果会话复制中省略了变量serialVersionUID,是否会出现任何问题?
提前致谢。
I would like to know if the object calling the session.setAttribute or session.getAttribute should be also needed to implement the "java.io.Serializable" in order to support the session replication?
for example
the class User is implemented the Serializable interface.
But if the class SomeUtilityClass that calling "session.setAttribute" is required to implement the Serializable interface.
/* Does it also need to implement the Serializable here? */
public class SomeUtilityClass{
public void test(HttpServletRequest request){
request.getSession().setAttribute("user", new User());
}
}
public class User implements Serializable{
private static final long serialVersionUID = 12345432534456654664L;
//skip the getter & setter...
}
And is there any problems if the variable serialVersionUID has been omitted for the session replication?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只有进入会话的对象才需要可序列化(即实现
Serializable
接口),这意味着设置为属性的任何对象,以及这些对象中包含的任何对象。
因此,如果您有一个类
,那么您的
Department
类还需要实现Serializable
类,这些类可以简单地在会话上进行调用(包括对
setAttribute
的调用)不需要实现Serialized
(包括SomeUtilityClass
)原因是会话中的任何内容都必须发送到其他 WebLogic 节点(可能运行在不同的服务器)。这意味着它需要通过网络发送您的
User
类,因此它需要能够将您的User
类转换为一系列可以发送的字节,然后在另一面再次将其变回真实物体。当您实现
Serialized
接口时,您就是在告诉 Java,您已经以可以将类转入这些字节(然后再转回来)的方式构建了类。如果你没有实现Serialized
,那么Java不知道是否可以这样做,所以它不会这样做。注意:类的代码不会被发送,只是发送其中字段的值。该代码已经在另一个节点上,因为它是已经部署到所有节点的应用程序的一部分。
您的实用程序类不需要通过网络。您还没有将该类的对象放入会话中,因此 WebLogic 不需要将其转换为字节(然后再返回_)作为会话复制的一部分。
唯一需要
可序列化
的是直接放入会话的对象(例如您的User)或间接(如
部门
示例)。仅在会话上进行调用的对象不需要
可序列化
。Only objects that go into the session need to be serializable (i.e. Implement that
Serializable
interface)That means any object that is set as an attribute, plus any objects that are contained inside those objects.
So if you have a class
then your
Department
class also needs to implementSerializable
Classes that simple make calls on the session (include calls to
setAttribute
) do not need to implementSerializable
(that includesSomeUtilityClass
)The reason is that anything that is inside the session has to be sent to the other WebLogic node (that is probably running on a different server). That means that it needs to send your
User
class over the network, so it needs to be able to turn yourUser
class into a series of bytes, that it can send, and then turn it back into a real object again on the other side.When you implement the
Serializable
interface you're telling Java that you've built your class in a way that it's OK for it to turn in into those bytes, (and back again). If you don't implementSerializable
then Java doesn't know whether it OK to do that, so it doesn't.Note: The code for the class isn't being sent around, just the values of the fields inside it. The code is already on the other node, because it's part of your application that is already deployed to all nodes already.
Your utility class, doesn't need to go over the network. You haven't put an object of that class into the session, so WebLogic doesn't need to turn it into bytes (and back again_) as part of session replication.
The only things that need to be
Serializable
are the objects that get put into the session either directly (like yourUser
) or indirectly (like theDepartment
example).Objects that simply make calls on the session do not need to be
Serializable
.如果您的实用程序类没有可跨服务器实例复制的状态,则无需将其序列化。至于 serialVersionUID 是 < a href="https://stackoverflow.com/questions/285793/why-should-i-bother-about-serialversionuid">整个新单独讨论。
If your utility class does not have state to be replicated across server instances then there is no need for it to be serializable. As for the serialVersionUID, is a whole new discussion on its own.