aspose file tools*
The moose likes Servlets and the fly likes Need to store non serialized object in session as attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need to store non serialized object in session as attributes" Watch "Need to store non serialized object in session as attributes" New topic
Author

Need to store non serialized object in session as attributes

Mohit Kumar Tayal
Greenhorn

Joined: Aug 27, 2008
Posts: 29
Hello,

I am working with an J2EE application and i need to save some object (non serialized) into session as an attribute. This is not creating problem on Myeclipse but after hosting the application if after creating some session we change some java files then it throws exception something want to say that we should not store the non serialized object into persisted sessions and session got empty.

Is there any way to forcibly save the non serialized object into session as attribute without creating this type of problems.

Please help me on this.

Thank You.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It depends on your app and your deployment.
If you're clustering with session replication, binding non-serializable objects to session will prevent replication from working.
Also if you need to be able to restart an application (or the container) without killing your users's session this could cause a problem as some containers (Tomcat for one) achieve this by serializing the whole session to disk when the app shuts down and then deserializes them when the app starts back up.
Also, if your container is storing session information in a database, it will need to be able to serialize them.

Why can't you implement Serializible?
What are you holding in these objects that can't be serialized?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mohit Kumar Tayal
Greenhorn

Joined: Aug 27, 2008
Posts: 29
Thanks Ben,

Actually i have stored the connection object in session within another object that is to be stored but i have removed this object from that class now.
Thank for the help.

Now i need one more help. as you have said that it is vendor specific where it will store the session data either in file on disk or in database, is it possible that tomcat save context attributes as well somewhere and reload them on server startup like session attributes.

I need to store some data in context as well.

Thank You
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat does not persist context scoped objects.
Mohit Kumar Tayal
Greenhorn

Joined: Aug 27, 2008
Posts: 29
Thanks Ben,

Is there any alternative way to achieve this something by using context linstener and overriding method named contextinitialized and contextdestroyed.
I think this will solve the problem but is there any direct way to save the object to some file and again load from the file in java.

Thank You.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
is there any direct way to save the object to some file and again load from the file in java.


Of course - make it Serializable, write it to disk and load it back whenever. Unless it is a really complicated object, you will be pleasantly surprised by how fast this is.

CAUTION: DO NOT do this with objects which should be managed by the container or which tie up system resources such as database connections.

Bill
Mohit Kumar Tayal
Greenhorn

Joined: Aug 27, 2008
Posts: 29
Thanks William,

You are right, i have achieved this by using file operations.

Thank You.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to store non serialized object in session as attributes