6

Possible Duplicate:
Why does HttpServlet implement Serializable?

This question suddenly came up couple of days ago in an internal discussion and we don't seem to find any suitable answer for the same . Can anyone point me in the right direction ?

The questions :

1) Why is HttpServlet in java implements serializable? I do not seem to find any logical reason for the same.

2) While trying to figure out this I looked at the api doc and found some thing interesting

public abstract class HttpServlet extends GenericServlet
implements Serializable

Now, what is of interest is that GenericServlet also extends Serializable. So both the parent and child class implements serializable . Isn't that an anti-pattern?

Community
  • 1
  • 1
Anubis05
  • 1,234
  • 2
  • 13
  • 17

2 Answers2

7

1) Why httpservlet in java implements serializable ? I do not seem to find any logical reason for the same.

To support clustering and serialization between VMs, passivation etc.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • what is passivation ? Never heard this term and google told something about oxide :( – Anubis05 Sep 12 '11 at 10:48
  • 1
    @ganguly.sarthak maybe you need to work on your googling skills : http://www.google.ie/search?sourceid=chrome&ie=UTF-8&q=passivation+servlet :-) – Ashkan Aryan Sep 12 '11 at 10:55
  • But if the parent class `GenericServlet` is Serializable, why mark `HttpServlet` Serializable again? – Sid Feb 02 '15 at 11:57
2

Another reason is that web-containers such as Tomcat calls some hooks when shutting down. Those hooks preserve the state of the applications/servlets on hdd, so when the web-container is restarted, the application doesn't lose it's state.

Romeo
  • 337
  • 1
  • 6
  • 17
  • 1
    Well a Servlet shall be thread-safe or at least has no conversation-state. So what is the need? – Amir Pashazadeh Jan 27 '13 at 10:58
  • @AmirPashazadeh A Servlet can have *lots* of conversation-state for each user. That is the entire point to a “session” object, tracking that state between Request/Response cycles. That state lives in memory while the Servlet container is running. When container exits, Poof!, memory cleared, session state gone. The idea in this Answer is about how *some* Servlet containers offer an optional feature to save that session state to storage via serialization to “dehydrate” the state data. Upon restart of the container, the session is rehydrated to let user continue their work where they left off. – Basil Bourque May 27 '17 at 22:59