Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why HttpServlet in java implements serializable? [duplicate]

Tags:

java

servlets

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?

like image 448
Anubis05 Avatar asked Sep 12 '11 10:09

Anubis05


People also ask

Why we need to implements Serializable?

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.

Why do we implement Serializable interface in Java?

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.

What happens if we do not implement Serializable?

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.

Why is Servlet Serializable?

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 Answers

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.

like image 136
Sean Patrick Floyd Avatar answered Sep 20 '22 11:09

Sean Patrick Floyd