aspose file tools*
The moose likes Beginning Java and the fly likes Why serialization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why serialization?" Watch "Why serialization?" New topic
Author

Why serialization?

Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi All,


It is said that serialization is used for storing state of object and also to transfer object over network

It is ok that we can store the state of object in the file so for that that class must implement the Serializable interface.

But i have one doubt that when session move from one server to another(in case of multiple server application and clustering we can say) , so it is going via network but our HttpSession Interface doesn't extend Serializable interface. So how this happens can anybody explain ???


Thanks in advance.


Regards,
Chetan
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hmm, but implementation of HttpSession might be implements Serializable

if you restart server, object in session wont get lost, if that objects implements Serializable
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi Seetharaman,


Is it like you are saying the "HttpSessionImpl" will be user defined class and where you can implement the HttpSession,Serializable Interface.

If that is the case then how JVM or load balancer or container whoever is responsible for the session transmission from one server to another will come to know that he needs to transfer the "HttpSessionImpl" object and not the "HttpSession" object



Thanks..!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Chetan, please KeepItDown, also in subject lines.

Chetan Dorle wrote:Is it like you are saying the "HttpSessionImpl" will be user defined class and where you can implement the HttpSession,Serializable Interface.

Not user defined - web server implementer defined. You don't create HttpSession implementations, but the creators of Tomcat, JBoss etc do.

If that is the case then how JVM or load balancer or container whoever is responsible for the session transmission from one server to another will come to know that he needs to transfer the "HttpSessionImpl" object and not the "HttpSession" object

The HttpSessionImpl object IS the HttpSession object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi Rob,


Please correct me if i am wrong

Web server take the "HttpSessionImpl" which is the session object and is serializable and hence seralize it and transfer over network.

But one query is that "Is it like Tomcat or JBoss convert the HttpSession to the HttpSessionImpl on there own because in code we actually write HttpSession and transfer over network and again at other server the HttpSessionImpl object convert to HttpSession"

Thanks...!!


Regards,
Chetan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42928
    
  68
There is no conversion happening:

Rob Spoor wrote:The HttpSessionImpl object IS the HttpSession object.


You should read up on interfaces: http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi Ulf ,


Again the same question arise , If "The HttpSessionImpl object IS the HttpSession object." and in the Code we actually get HttpSession object through request.getSession(); so how can container serialize him.

If you elaborate that will be great help.


Thanks.
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
For starters HttpSessionImpl is serializable. So you should be fine

If in the future however, you are faced with an issue of having a non serializable object to be passed over the wire, there is a standard procedure to follow.
1) find out all the objects in the class(fields) which you need on the other end.
2) Make sure all are serializable(else you have to recursively define them)
3) Store their value.
4) "Override" the callback methods writeObject(...) to write these values
5) "Override" the callback method "readObject(...) to read these values and reconstruct the object on the other end

Alternatively you can use the externalizable interface to do the same
Look here for mnore info
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42928
    
  68
I don't understand the question. HttpSession implements Serializable, so what do you mean by "how can [the] container serialize [it]"? It does so by using the standard JRE serialization mechanism, like any other object would be serialized. Or are you asking how serialization works in general?
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
oh yea.. btw... look at polymorphic references... the code you are expecting uses them
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi Ulf,


HttpSession interface doesn't extend Serializable Interface .So my question is if it doen't implement Serializable interface So how HttpSession transfer from one server to another ?




Thanks
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
The HTTPSession doesnot extend serializable... But the object which you are using is not HTTPSession... it is an implementation of HTTPSession... most prolly HTTPSessionImpl. ... which does implement serializable...
As i said, read about polymorphic references.
A parent (interface) reference can refer to a child(implementation) object. Which is what is happening in your case.
So dont treat the object as an HTTPSession object. There is no such thing. It is by all right a HTTPSessionImpl object which can pose (due to laws of inheritance) as an HTTPSession object whenever required, because it "IS A" HTTPSession
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

The server does it. It uses programming. This programming may or may not involve serialization. It may or may not involve transferring an object which implements both HttpSession and Serializable. You seem to be making a big meal of this rather minor question.
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Thanks Gaurav & Paul ,


You clicked my exact problem and thank you very much for your suggestion.


Regards,
Chetan
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi All ,

I have one more doubt , is that can we write multiple objects in the same file or we need to write them in the different files and which

is the best practice that Sun recommends for writing object .

Thanks....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why serialization?