• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does HttpServlet implement Serializable?

 
Hima Mangal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am fairly new to servlets and was going thru the javadocs to familiarise myself with the methods when I saw the HttpServlet implements serializable.

I would like to know why is it so. Is it because servlet information needs to be persisted across multiple requests, or am I confusing it with HttpSession?

Would be great if someone can let me know the answer.

Thanks
~Hima
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter den Haan gives an authoritative answer in this thread from last week.

Jules
 
Hima Mangal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jules, thanks for the reply, but I am afraid I wasnt able to find the thread you are referring to. Can you please direct me to the relevant thread?

Thanks
~Hima
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click on this link: When to use Serializable. If that doesn't work for some reason then search for the topic "When to use Serializable"; the thread was initiated by Martin Lira.

Jules
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a slightly different question though. To be honest, I'm not at all sure, and the spec doesn't shed an much light on the matter either. Servlets are not marshalled between containers like the HttpSession contents is in a distrubuted setup. The best excuse I can think of is so that you can safely bind such servlets in the HTTP session, but I'm not sure why you would want to do that.

Anyone?

- Peter
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, my mistake. Always read the question ... and the answer, eh?! :roll:

Jules
 
Hima Mangal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jules, no problems, that thread was useful in another way!


Peter, i agree with what you say about marshalling, but can you please elaborate on 'binding servlets in the HTTPSession'? Thanks.

Now only if someone can tell me the answer!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64960
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but can you please elaborate on 'binding servlets in the HTTPSession'?


Doing something like



which makes my teeth hurt just looking at it. As Peter said, it's a pretty pointless thing to do.

I can't think of any good reasons for the Serializable implementation either. Since the vast majority of servlets have no non-static member variables (you know, that whole thread safety thing), what is there to serialize?

I can only imagine that the original author envisioned a possible scenario where it might need to be used. What that scenario was...
 
Hima Mangal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks BB. I agree that binding a servlet to a session makes for one reason for HttpServet to implement Serializable. But since this must be rarely used, if any, is there any other reason anyone can think of?

I dont want to sound like a stuck record, but would appreciate if anyone can answer the Q!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attempted an answer in this thread but it's just conjecture. You could try posting your question in one of the Sun forums. You never know; you might get an answer from the original developer!

Jules
 
Hima Mangal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jules! I think I will follow your suggestion. Will let everyone know if I hit paydirt. Thanks for all the help.

~Hima
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the answer. James Duncan Davidson <james.davidson@ENG.SUN.COM> writes:
No, a servlet could be run in a distributed fashion without being serializable as it's the data about sessions and context that has to be
shared.

The interfaces specify serializable so that a servlet can be serialized into a .jar file by a tool and reconsituted later by a servlet engine. This behavior however is not yet well specified.
Makes sense, in a slightly perverted way. I don't think anyone is using this really.

- Peter
 
Ajith Anand
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be useful in clustered application server scenarios.... Where the servlets need to be moved around from one server to the other .. ..a servlet pooled, load balancing scenario...

or to implement somekind of a crash-recovery mechanism....MAYBE..wild guesses....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajith And Ajith And:
Would it be useful in clustered application server scenarios.... Where the servlets need to be moved around from one server to the other ..
No. 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. The GenericServlet convenience implementation is, but you can implement the Servlet interface yourself.

- Peter
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<dumb-question>So, if a servlet is stateless, what would get serialized to the JAR file in the previos explanation?</dumb-question>
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "stateless" nomer applies to conversational state only, i.e. request or session state. There may still be configuration information. I think the idea is that you configure your servlet using a tool and then serialize it out complete with its configuration information. Once more this is to some extent analogous to SLSBs which may maintain cached results from JNDI lookups and the like.

- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic