HttpServlet为什么实现Serializable? [英] Why does HttpServlet implement Serializable?
问题描述
在我对Servlet的理解中,该Servlet将由Container实例化,其init()
方法将被调用一次,并且Servlet将像单例一样生活,直到JVM关闭.
In my understanding of Servlet, the Servlet will be instantiated by the Container, its init()
method will be called once, and the servlet will live like a singleton until the JVM shuts down.
我不希望我的servlet被序列化,因为当应用服务器恢复或正常启动时,它将被重新构造.该servlet不应该包含特定于会话的成员,因此将其写入磁盘并重新实例化是没有意义的. 有实际用途吗?
I do not expect my servlet to be serialized, since it will be constructed new when the app server recovers or is starts up normally. The servlet should hold no session-specific members, so it does not make sense for it to be written to disk and re-instantiated. Is there a practical use for this?
我担心的是,我在其中放置了一些不可序列化的字段,然后我的应用程序会在生产环境中神秘地失败,在该生产环境中将进行不同类型的会话复制.
My concerns are, that I put some non-serializable fields within there and then my app will mysteriously fail in a production environment where a different sort of session replication will take place.
推荐答案
从技术上讲,我认为允许servlet容器以与EJB会话bean相似的方式将servlet对象钝化"到磁盘.因此,您正确的问题是您的应用是否会由于无法序列化的字段而失败.
Technically, I believe the servlet container is allowed to "passivate" the servlet object to disk, in a similar way that EJB session beans can be. So you're correct to ask the question if your app will fail due to non-serializable fields.
实际上,我从未听说过有容器可以这样做,所以实际上这只是早期J2EE糟糕时期的遗留物.我不用担心.
In practise, I've never heard of a container doing this, so it's really just legacy baggage from the bad old days of early J2EE. I wouldn't worry about it.
这篇关于HttpServlet为什么实现Serializable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!