Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why HttpServlet implements Serializable interface

 
Bishnu Mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GenericServlet implements Serializable and HttpServlet extends GenericServelet then Serialization facility available to HttpServlet.. Then why HttpServlet implements Serializable interface?
 
Stefan Evans
Bartender
Posts: 1779
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"


-Cameron
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic