File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why getting Exception...

 
Annie Naqvi
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sarath Mohan
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i fear that Binu is wrong
 
Annie Naqvi
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic