• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why getting Exception...

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am having problem while retrieving Object of class A from Servlet Context.Plz help me.
A part of code is here:
<code>
1. ServletContext context = getServletContext();
2. Enumeration e=context.getAttributeNames();
3. while(e.hasMoreElements())
4. {
5. A sm = (A)e.nextElement();
6. }
</code>
I am getting ClassCastException at line 5.I have stored the objects in ServletContext of Class A(a public class in the same server class path.)
Any help will be acknowledged.
Regards,
Annie.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Annie,
Attribute has a name and corresponding value.
where name is always a String type object.
Using that name we can retrieve the value (the actual object you may store)
So the code needs to be changed:
2. Enumeration e=context.getAttributeNames();
3. while(e.hasMoreElements())
4. {
5. String sm = (String)e.nextElement();
A objA= (A) context.getAttribute(sm);
6. }
But here you will get only the last object in objA.
Cheers
Binu
 
Annie Naqvi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanx Binu for reply.
One more thing to clear from anyone here that in the example coded above I think that i'will got each object one by one as nextElement() will give the next object in the context.So why binu said that in the above case i will got only last element.
Thanx in @dvance.
Annie.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i fear that Binu is wrong
 
Annie Naqvi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Anoop plz tell me the right explaination as i am new to servlet.
Thanx.
Annie.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Annie,
The code which binu has given is perfect but his comment
"But here you will get only the last object in objA."
is wrong
If i am wrong I hope binu will correct me
 
Annie Naqvi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again..
Binu and Anoop thanx for your answers..
But I am not out of problem yet
I have done same as Binu said but now I am getting
"java.lang.ClassCastException: org.apache.catalina.loader.StandardClassLoader"
Oh my head is not responding now as i am trying since last night to solve the prob.
I will really appreciate any guidence in this respect.
Regards,
Annie.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think binu's comment is correct. He said objA will refer to only the last object that is stored in context. If you call objA out side of the while loop, you'll see that objA only refers to the last object in context.
 
reply
    Bookmark Topic Watch Topic
  • New Topic