File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes java beans and serialized beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "java beans and serialized beans" Watch "java beans and serialized beans" New topic
Author

java beans and serialized beans

riya s
Ranch Hand

Joined: Feb 28, 2003
Posts: 110
Until I read SCWCD Kit I thought that java beans had to be serialised.. even if it had get and set methods...
Java beans has to fulfill:
1) no arg constructor
2) get and set methods
3) serialized...
Before adding any bean object to session I serialize the object..
But now I see that serialize means you can store the bean in the web-inf\classes folders...
Do we use serialized beans just to store something in web-inf\classes or are they used to ensure proper transer to next page..?
Bradley Johnson
Greenhorn

Joined: Aug 18, 2003
Posts: 5
In one situation I serialized my java beans because I had a database that was under powered and could not keep up with real time demand. Originally the data was on a Sybase database but was moved to a SQL-Server database. The system was built to give real time results so it would query the database each time someone requested data.
Now real time data is not so important because of the cost involved. At night the system queries the database and builds the Java beans and then serializes them. When people use the system it pulls the data from the serialized bean instead of going to the database.
Using java beans to hold the data saved me tons of time in reprogramming the system to deal with a slow database. All I had to do was add methods to my data access objects to pull data from the serialized copy while keeping the methods that query the database and build the java beans.
-brad
Tieyi Guo
Greenhorn

Joined: Oct 29, 2001
Posts: 15
Before adding any bean object to session I serialize the object..
But now I see that serialize means you can store the bean in the web-inf\classes folders...
Do we use serialized beans just to store something in web-inf\classes or are they used to ensure proper transer to next page..?

I am not clear why you need to serialize your object before you put it in your session and I am curious how did you do that. As I understand two most often case for serializing objects are writing objects to second storage like files or transfer objects arcoss the wire like writing to a remote URL. Never heard that somebody put a serialized object into session. And I do not think it is necessary to do that to ensure the session object will be transfered propably between pages.
When you talk about putting a serialized object into session, you are talking about putting a serialized object(which is in bytes blob, virtually) then put it into another location in the memory. If you are not a session replication environment, I really do not see what is the reason of doing that.
riya s
Ranch Hand

Joined: Feb 28, 2003
Posts: 110
Thanks Bradley for a very good example!
how does the system build java beans at night and serialize them.. are you using normal classes or servlets for this?
Since the serialized object resides in web-inf\classes and for every person I assume you would have a different .ser file ...How many people can pull the data from the serialized bean.. Is there a limit to it ?
riya s
Ranch Hand

Joined: Feb 28, 2003
Posts: 110
Hi Tieyi,
If I have a user bean (serialized).. I add name,tel, etc to bean.
Add the bean to session. I add them to session because I want to ensure I dont loose the information .. which I could (??).. if I added a object like String to the session.
I guess I dont need to serialize the bean then (I assumed that beans had to implement serializable to be beans...)
Just adding get and set methods makes them a bean and no arg constructor?
What do you mean by > transer objects across the wire like writing remote url?
Do you mean using RMI or URL rewriting which creates dynamic URLs?
Bradley Johnson
Greenhorn

Joined: Aug 18, 2003
Posts: 5
At this point the system waits for someone to access that particular piece of data and checks to see what time it is. If it is at night it gets the data out of the database to make the bean, then serializes it to the hard drive and then displays it to the user. This way old data that is never used is not refreshed.
I have plans to put a threaded timer object in to get the data out after a certain amount of time instead of relying on someone trying to get the data.
To build the bean and serialize it I am using a data access object, which is just a normal class. But this is called into existence by a user accessing a jsp page which uses beans passed to it by a servlet. The servlet gets the bean from an EJB and then the EJB gets it from the DAO.
I do not put the .ser file in web-inf/classes. I have it make its own directory at a higher level and put the data there.
There is not one .ser file per user. Of course once 10 users get a copy of the bean into their session there will be 10 copies of the original bean but still only one serialized bean. The user cannot get data out of a serialized bean, it has to be deserialized first.
It helps that this application is just getting a read only copy of the data. If the contents of the bean needed to be saved back to the serialized copy there would be other issues to deal with.
I am not sure how many users can deserialize a serialized bean at one moment. It is an interesting question. I would guess the answer is dependent on how your particular OS implements threads and multi-tasking. I have 9000 users with two clustered java app servers and it has not been a problem. It is faster than getting the data out of our database.
Bradley Johnson
Greenhorn

Joined: Aug 18, 2003
Posts: 5
Hi Priya,
Beans implement serializable but that does not mean that you have to cause them to serialize. One reason I would programatically serialize a user bean was so when the user came back after a few days their profile settings would still exist. Unless I had a database, if I had a database that performed well I would put the data there.
Beans need to implement serializable so the Java can decide if it needs to serialize them for particular reasons.
Originally posted by Priya Shah:
Hi Tieyi,
If I have a user bean (serialized).. I add name,tel, etc to bean.
Add the bean to session. I add them to session because I want to ensure I dont loose the information .. which I could (??).. if I added a object like String to the session.
I guess I dont need to serialize the bean then (I assumed that beans had to implement serializable to be beans...)
Just adding get and set methods makes them a bean and no arg constructor?
What do you mean by > transer objects across the wire like writing remote url?
Do you mean using RMI or URL rewriting which creates dynamic URLs?
Bradley Johnson
Greenhorn

Joined: Aug 18, 2003
Posts: 5
http://javaboutique.internet.com/resources/books/WebDev_JSP2/webdev2_3.html
Here is a document that gives one reason
"Some servers support indefinite, long-term session persistence by writing any session data (including beans) to disk between server shutdowns. When the server comes back up, the serialized data is restored. This same reasoning applies to servers that support clustering in heavy traffic environments. Many of them use serialization to replicate session data among a group of web servers. If your beans do not implement the Serializable interface, the server will be unable to properly store or transfer your beans (or other classes) in these situations."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java beans and serialized beans