File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Why HttpServlet implements Serializable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why HttpServlet implements Serializable interface" Watch "Why HttpServlet implements Serializable interface" New topic

Why HttpServlet implements Serializable interface

Bishnu Mishra

Joined: Sep 01, 2006
Posts: 2
GenericServlet implements Serializable and HttpServlet extends GenericServelet then Serialization facility available to HttpServlet.. Then why HttpServlet implements Serializable interface?
Stefan Evans

Joined: Jul 06, 2005
Posts: 1517
Why not?
It is probably more for documentation purposes than anything else - ie just so the javadoc will explicitly state it, rather than the reader having to KNOW that it extends a class that implements serializable.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Serializable - The Breakfast Interface

Because Serializable is a marker interface, any class that is indeed serializable should include this interface in its class declaration.

I can create a class with all, boring Java data types and call it serializable. Then I can subclass it and add an instance variable that is of type DataSource or DatabaseConnection, which isn't declared as being transient, and isn't serializable. In this case, I have extended a class that IS serializable, but my own class is indeed NOT serializable.

Does that make any sense?

By the way, if you extended a Serializable class, but added instance variables that weren't Serializable, would you be a "serial killer?"

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I'm not satisfied with the above answers
There must be a decent reason to decide to set this class Serializable. Servlets are not supposed to hold states, so I can't understand either why it has to be Serializable. Any relation to distributed environments ?

[My Blog]
All roads lead to JavaRanch
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Servlets are allowed to hold state, they just shouldn't hold user state. I can think of better ways to do it, but a cluster may require Servlets to be Serializable.
I agree. Here's the link:
subject: Why HttpServlet implements Serializable interface
It's not a secret anymore!