JSP 中的 Bean 序列化
为什么有时教程让 Bean 实现 Serialized 对象,而另一些则不然? 我知道当我想通过网络发送对象时应该将其序列化,这是否证明会话中使用的每个 bean 都应该实现可序列化对象,而 JSP 页面中定义的 bean 则不应该实现,因为它们不是使用 HTTP 请求集传输的
Why some times tutorials make beans implement Serializable object and others do not?
I know that object should be serialized when I want to send it through a network, so does that prove that each bean used in sessions should implements Serializable objects and beans defined in JSP pages should not since they are not transferred using HTTP requeset
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎相信会话中的对象是在http传输中发送到客户端的?事实肯定不是这样的。传输的只是会话 ID(通常在 cookie 中)。 servlet 容器(例如 Tomcat)仅在内存中保存会话对象(是否为 bean),并通过会话 id 进行索引。
此外,序列化不仅适用于网络传输,还适用于保存/加载到持久存储(例如磁盘)。
现在,许多 servlet 容器通常允许(根据设置)将 Session 对象保留到磁盘,以便它们能够在应用程序服务器重新启动后继续存在。对于这种情况,必须让会话对象可序列化。
不管怎样,实现 Serialized 接口对于每个 java bean 来说都是一件好事,而且通常很容易。
You seem to believe that objects in a session are sent to the client in the http transfer? That's certainly not the case. What is tranferred is only the session id (typically in a cookie). The servlet container (eg Tomcat) just keeps in memory the session objects (beans or not), indexed by the session id.
Besides, serialization does not only apply to network transfer, it also applies to save/load to persistent storage (eg disk).
Now, many servlet containers usually allow (depending on the settings) to persist the Session objects to disk so they can survive a app-server restart. For that scenario, to have your session objects serializable is a must.
Anyway, implementing the Serializable interface is a nice thing to have for every java bean, and usually it's easy.
根据定义,格式良好的 Java Bean 实现
Serialized
(只是一个标签无论如何接口)或Externalized
(自1.4起)因此,如果您的bean类没有,那么它的格式不正确。然而,有太多确实实现了 Serialized,如果 bean 有一个众所周知的父类,您通常可以摆脱它。
By definition, a well-formed Java Bean implements
Serializable
(which is just a tag interface anyway) orExternalizable
(since 1.4) So if your bean class doesn't, it's not well-formed.However, there's so much that does implement Serializable you can often get away with it if the bean has a well-known parent class.