File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Why does HttpServlet implement Serializable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why does HttpServlet implement Serializable?" Watch "Why does HttpServlet implement Serializable?" New topic
Author

Why does HttpServlet implement Serializable?

Hima Mangal
Greenhorn

Joined: Oct 17, 2002
Posts: 9
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

Joined: Aug 02, 2004
Posts: 823
Peter den Haan gives an authoritative answer in this thread from last week.

Jules
Hima Mangal
Greenhorn

Joined: Oct 17, 2002
Posts: 9
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

Joined: Aug 02, 2004
Posts: 823
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 02, 2004
Posts: 823
Hmmm, my mistake. Always read the question ... and the answer, eh?! :roll:

Jules
Hima Mangal
Greenhorn

Joined: Oct 17, 2002
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hima Mangal
Greenhorn

Joined: Oct 17, 2002
Posts: 9
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

Joined: Aug 02, 2004
Posts: 823
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

Joined: Oct 17, 2002
Posts: 9
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 30, 2004
Posts: 40
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....


LXI Technologies P Ltd
[url]www.lxisoft.com[/url]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 02, 2004
Posts: 823
<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

Joined: Apr 20, 2000
Posts: 3252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does HttpServlet implement Serializable?