my dog learned polymorphism*
The moose likes Servlets and the fly likes question about setAttribute() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "question about setAttribute()" Watch "question about setAttribute()" New topic
Author

question about setAttribute()

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi all,
when we use setAttribute() on any object (request,session etc) how does it (the attribute we set) get stored?
does it get serialized or not? (or does it need to be Serializable or not to be precise?). i doubt, it need not be serializable but i'm not sure.
please clarify my doubt.
regards
maulin
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
It should implement Serializable.
You may run into problems later if you don't...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I forgot: The attrib gets set as in a hash table: A dictionary. Name/value pair.
Notice that the object gets stored as an Object so you may have to cast when you get it back.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi tony,
thanks for the reply. i asked the question to get cleared about one the of concept. it is related to MY reply for the one of the post at here
as u can see the problem of having ClassCastException, i assumed that similar thing will happen if i write an object to the applet from the servlet and access it using readObject() in the applet and then cast it back to the original type. means, it should raise ClassCastException if the class loaders for the applet and the servlet are different (which are different i guess. i have to yet make sure).
what i have is,
- applet having class "TestClass" in the applet code itself (its not an innerclass though, its two classes declare in one .java file).
- this applet connects to the servlet using URLCOnnection
- the servlet has the same class "TestClass" in the Servlet code (two classes declared in one .java file)
- the servlet returns an object of "TestClass" to the applet
- the applet gets back that object and re-cast it to the "TestClass" object
Here, i DON'T get ClassCastException when i re-cast TestClass object in the applet. why?
if the two classloaders (for the applet & the servlet to which i connect) are different and both of them have different copies of the "TestClass.class" then why it doesn't give me ClassCastException?
the last post from me for the same thread i gave link above is not correct though (saying - there is a problem with the webserver). i agree that if the object isn't serializable then setAttribute() might not work properly...
hope u understand my question. i have been thinking of the reason for sometime but i am not able to figure out why i'm not getting ClassCastException in the applet....thats why i was plunging myself on serialization fundamentals in setting attributes etc and wanted to make sure what is happening actually...
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about setAttribute()