This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Why getting Exception... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why getting Exception..." Watch "Why getting Exception..." New topic
Author

Why getting Exception...

Annie Naqvi
Ranch Hand

Joined: May 04, 2001
Posts: 39
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

Joined: Mar 17, 2001
Posts: 213
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

Sarath Mohan
Annie Naqvi
Ranch Hand

Joined: May 04, 2001
Posts: 39
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

Joined: May 03, 2001
Posts: 163
i fear that Binu is wrong


I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Annie Naqvi
Ranch Hand

Joined: May 04, 2001
Posts: 39
Hi,
Anoop plz tell me the right explaination as i am new to servlet.
Thanx.
Annie.
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
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

Joined: May 04, 2001
Posts: 39
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

Joined: Apr 06, 2001
Posts: 371
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
 
subject: Why getting Exception...
 
Similar Threads
Tag Files and ServletContext
problem while loading the welcome page
help please - webapplication stress tool
Error while deploying struts application on Weblogic server
request.getAttribute() returns null in jsp