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?
Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class. Serializable classes are useful when you want to persist instances of them or send them over a wire.
Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.
If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.
The Servlet specification mentions nothing of the kind, and servlets are assumed to be stateless anyway. It's like stateless session beans in the EJB tier: because they are interchangeable you don't need to marshal them across the wire. Also note that there is no requirement for servlets to be Serializable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With