HttpServlet为什么要实现serializable?

boris2002 2009-11-23 05:35:15
两个问题不理解:
HttpServlet为什么要实现serializable?在什么情况下,servlet会被序列化?
如果未显示定义serialVersionUID,系统会用什么算法给指定一个?
谢谢!
...全文
889 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
longtenggdf 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 hbwhwang 的回复:]
我认为主要的原因是servlet容器可能会钝化servlet,把不活跃的servlet暂时持久化到IO设备

[/Quote]
这个应该是正解,32楼同学给出的文档中已经说明了。
The servlet engine is not required to keep a servlet loaded for any period of time or
for the life of the server. Servlet engines are free to use servlets or retire them at any
time. Therefore, you should not rely on class or instance members to store state
information.
When the servlet engine determines that a servlet should be destroyed (for example,
if the engine is shut down or needs to conserve resources), the engine must allow the
servlet to release any resources it is using and save persistent state. To do this, the
engine calls the servlet’s destroy method.
The servlet engine must allow any calls to the service method either to complete
or to end with a time out (as the engine defines a time out) before the engine can
destroy the servlet. Once the engine destroys a servlet, the engine cannot route any
more requests to the servlet. The engine must release the servlet and make it eligible
for garbage collection.

容器不要求每个servlet都一直维持在内存,可以钝化,servlet也支持多种类加载方式,包括反序列化。

longtenggdf 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 xuexijava 的回复:]
引用 12 楼 closewbq 的回复:
个人之间,serlvet应用在javaweb中,可能需要在网络中进行传输。而网络船传输过程中肯定是需要协议的,我认为将对象序列化,就是解决这个问题的。
---》什么时候被序列化?
应该是服务器加载serlvet的时候。
---》如果未显示定义serialVersionUID,系统会用什么算法给指定一个?
具体什么算法就不知道了,反正系统肯定会默认分配各serialVersionUID。以便反序列化的时候更快速的找到对象。(serialVersionUID这个东西更像是数据库中的一个索引,通过索引找到具体的数据)。
Mark
[/Quote]
如果servlet都需要在网络中传播了,那还要BS架构做什么!!有意义么?安全么?
xuexijava 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 closewbq 的回复:]
个人之间,serlvet应用在javaweb中,可能需要在网络中进行传输。而网络船传输过程中肯定是需要协议的,我认为将对象序列化,就是解决这个问题的。
---》什么时候被序列化?
应该是服务器加载serlvet的时候。
---》如果未显示定义serialVersionUID,系统会用什么算法给指定一个?
具体什么算法就不知道了,反正系统肯定会默认分配各serialVersionUID。以便反序列化的时候更快速的找到对象。(serialVersionUID这个东西更像是数据库中的一个索引,通过索引找到具体的数据)。




[/Quote]

Mark
longtenggdf 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 kky2010_110 的回复:]
引用 19 楼 villagehead 的回复:
引用 17 楼 fengjie926 的回复:
个人认为原因有2
  1:javaEE规范要求
  2:当服务器出现意外情况而”down“了 可以将缓存中的数据写入硬盘 当重启的时候可以恢复当时状态

1我看了j2ee api,
是从javax.servlet.GenericServlet就实现了Serializable接口
有个see also
连接到了
http://java.sun.com/javaee/5/docs/api/serialized-form.html#javax.servlet.GenericServlet
不过没啥东西也。

牛人来解释一下吧


2靠谱

不过http协议本身是无状态的,
难道servlet钝化了再恢复回去
request和response还能用?
尤其是response,人家客户端没准都关了,还有啥用

继续顶

good luck


我觉得2正确,以前有个项目没有序列号的时候,重启服务器是要重新登录的,序列化以后,重启后没必要重新登录了
[/Quote]
服务器的重启分两种,热启动和冷启动
热启动的话直接restar,我的理解是服务器并没有完全关闭,而是将内部资源刷新后重新加载,这个时候session啊啥的还在内存中得以维持。所以我想你说的重启服务器应该是热启动。这个跟序列化应该没什么关系。
冷启动的话,先关闭服务器,然后再开启服务器。这个时候如果session还在么?还不需要重新登录么?你试一下!
bayougeng 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yejingtao703 的回复:]
大家都知道jsp本质是一个servlet吧,jsp就是通过网络把页面从服务器端带到客户端,所以也可以说是servlet从服务器端传送到客户端,由此可见servlet是经过网络传输的,所以必定要实现Serializable,因为只有实现了Serializable才能被序列化,才能在网络上传输
[/Quote]
这位兄台更强,jsp也得去客户端执行了。
villagehead 2009-11-26
  • 打赏
  • 举报
回复
是说
“各位servlet实现容器,你们爱咋整咋整吧,
反正有这个接口可以让你们折腾”

是这个意思吗?

E文水平太差,没太看出来

谁给解释解释吧

good luck
shine333 2009-11-26
  • 打赏
  • 举报
回复
继续补充,标准允许“钝化”:
How a Servlet is Destroyed
The servlet engine is not required to keep a servlet loaded for any period of time or
for the life of the server. Servlet engines are free to use servlets or retire them at any
time. Therefore, you should not rely on class or instance members to store state
information.
When the servlet engine determines that a servlet should be destroyed (for example,
if the engine is shut down or needs to conserve resources), the engine must allow the
servlet to release any resources it is using and save persistent state. To do this, the
engine calls the servlet’s destroy method.
The servlet engine must allow any calls to the service method either to complete
or to end with a time out (as the engine defines a time out) before the engine can
destroy the servlet. Once the engine destroys a servlet, the engine cannot route any
more requests to the servlet. The engine must release the servlet and make it eligible
for garbage collection.
panhaichun 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 villagehead 的回复:]
引用 17 楼 fengjie926 的回复:
个人认为原因有2
  1:javaEE规范要求
  2:当服务器出现意外情况而”down“了 可以将缓存中的数据写入硬盘 当重启的时候可以恢复当时状态

1我看了j2ee api,
是从javax.servlet.GenericServlet就实现了Serializable接口
有个see also
连接到了
http://java.sun.com/javaee/5/docs/api/serialized-form.html#javax.servlet.GenericServlet
不过没啥东西也。

牛人来解释一下吧


2靠谱

不过http协议本身是无状态的,
难道servlet钝化了再恢复回去
request和response还能用?
尤其是response,人家客户端没准都关了,还有啥用

继续顶

good luck
[/Quote]

嗯,这个是有道理的,request和response那是肯定不能的,本来就不在servlet中。
可能servlet中有缓存的对象吧,比如说某个字段保存了该servlet被访问的统计量。
panhaichun 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 hbwhwang 的回复:]
我认为主要的原因是servlet容器可能会钝化servlet,把不活跃的servlet暂时持久化到IO设备

[/Quote]

嗯,这么说也靠谱,不过一个servlet也没占多少空间,没必要省那内存
villagehead 2009-11-26
  • 打赏
  • 举报
回复
也就是说为了开发servlet引擎/容器而设计的?

sun考虑的还真周到...

good luck
shine333 2009-11-26
  • 打赏
  • 举报
回复
找了个老点的Spec.
http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf

里面对于Servlet的Lifecycle有如下定义:
How a Servlet is Loaded and Instantiated
The servlet engine instantiates and loads a servlet. The instantiation and loading can
occur when the engine starts, when it needs the servlet in order to respond to a
request, or any time in between.
The servlet engine loads a servlet using the Java class loading facility. The servlet
engine can load the servlet from the local file system, a remote file system, or a
network source
.

也就是各个ServletEngine(不仅仅是HttpServlet)可以自由的选择不同的方式来进行类加载,比如使用
自定义的ClassLoader进行类的加载,同样,也可以使用自定义的手段,进行实例化,比如反序列化操作。
hbwhwang 2009-11-26
  • 打赏
  • 举报
回复
我认为主要的原因是servlet容器可能会钝化servlet,把不活跃的servlet暂时持久化到IO设备
ABAP1314 2009-11-26
  • 打赏
  • 举报
回复
学习了
bayougeng 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 realreachard 的回复:]
HttpServlet 要通过网络传输,传输要转化成流的形式, 为了能将拿到的流转化回 HttpServlet对象,所以要序列化

[/Quote]
照你这个意思,服务器端生成了servlet,然后传递到客户端去执行是么?
还是说,同一台机器上传递变量要通过网络,而不是直接给内存地址?
gggdwdn 2009-11-26
  • 打赏
  • 举报
回复
UP学习
villagehead 2009-11-26
  • 打赏
  • 举报
回复
回ls
那就是为了序列化...不,是钝话session/或者说是写cookies了?

接着顶

good luck
kky2010_110 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 villagehead 的回复:]
引用 17 楼 fengjie926 的回复:
个人认为原因有2
  1:javaEE规范要求
  2:当服务器出现意外情况而”down“了 可以将缓存中的数据写入硬盘 当重启的时候可以恢复当时状态

1我看了j2ee api,
是从javax.servlet.GenericServlet就实现了Serializable接口
有个see also
连接到了
http://java.sun.com/javaee/5/docs/api/serialized-form.html#javax.servlet.GenericServlet
不过没啥东西也。

牛人来解释一下吧


2靠谱

不过http协议本身是无状态的,
难道servlet钝化了再恢复回去
request和response还能用?
尤其是response,人家客户端没准都关了,还有啥用

继续顶

good luck
[/Quote]

我觉得2正确,以前有个项目没有序列号的时候,重启服务器是要重新登录的,序列化以后,重启后没必要重新登录了
kky2010_110 2009-11-26
  • 打赏
  • 举报
回复
http://blog.csdn.net/kky2010_110/archive/2009/11/20/4839807.aspx
java为甚序列化,为什么不是所有的类都序列化。
这里转载了一篇文章,可是更深层的东西不知道了,那个大侠说一说
  • 打赏
  • 举报
回复
1111111111111111111111111111111111
villagehead 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 fengjie926 的回复:]
个人认为原因有2
1:javaEE规范要求
2:当服务器出现意外情况而”down“了 可以将缓存中的数据写入硬盘 当重启的时候可以恢复当时状态
[/Quote]
1我看了j2ee api,
是从javax.servlet.GenericServlet就实现了Serializable接口
有个see also
连接到了
http://java.sun.com/javaee/5/docs/api/serialized-form.html#javax.servlet.GenericServlet
不过没啥东西也。

牛人来解释一下吧


2靠谱

不过http协议本身是无状态的,
难道servlet钝化了再恢复回去
request和response还能用?
尤其是response,人家客户端没准都关了,还有啥用

继续顶

good luck
加载更多回复(18)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧