wood burning stoves 2.0*
The moose likes Servlets and the fly likes a question about the ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "a question about the ServletContext" Watch "a question about the ServletContext" New topic
Author

a question about the ServletContext

vrussell
Greenhorn

Joined: Jun 04, 2002
Posts: 4
I create the class Video.
in the first servlet, I create several Video objects and put them into a Vector object, and then put the Vector object into the servletContext;
in the second servlet, I get the Vector object from the servletContext,it is okay, then when I get the Video object from the Vector and cast them back to Video, the run time exception raises.
Who chould tell me why?
following is the error message on the browser:
java.lang.ClassCastException: Video
at AddVideoServlet.doGet(AddVideoServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:309)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
at com.evermind[Orion/1.5.2 (build 10460)]._cxb._abe(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._cxb._uec(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._io._twc(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._io._gc(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._if.run(Unknown Source)

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
How are you setting and retrieving it from the servlet context? Apparently, when you retrieved it, it did not contain a video object.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi vrussell ,
where u have that Video object(i mean class file)? in /servlet directory with other servlets??
if so move it from there to the server classpath. it should work. i tried put that in my /servlet directory. it compiles. but it gives me runtime ClassCastException which is precisely u r getting. i also did put a Vector in the servletcontext() and so on...
the reason of this excepiton is very interesting...few points which we need to know first.
-EACH SERVLET (or any other class in the /servlet dir) is loaded by a DIFFERENT (New) ClassLoader.
-whatever classes which are there in the WebServer classpath are loaded using Primordial ClassLoader.
-if two different class loaders loads THE SAME class on their own then that class type WILL NOT match.
so, here i'm having say two servlets as below,


now, consider the following cases,
1. i have put TestClass.class in the /servlet dir.
Output:
Vector NOT NULL
Not matching instance for casting
2. i have put TestClass.class in WebServer's classpath
Output:
Vector NOT NULL
TestClass
in case 1, two servlets loaded by a different classloader and TestClass.class also gets loaded by corresponding classloader that loaded the servlets. so the type differs of TestClass.class.
in case 2, two servlets loaded by a different classloader but TestClass.class is in the CLASSPATH and so gets loaded by a primordial classloader in both of the servlets hence the type of TestClass matches and (o instanceof TestClass) returns true.
so, i guess i'm clear in my explanation. the point i forgotten was "any class in the standard classpath gets downloaded by a primordial classloader" ...
regards
maulin
vrussell
Greenhorn

Joined: Jun 04, 2002
Posts: 4
Hi, Maulin,
Thank you very much for you help and the detailed explanation!!
I try
System.out.println(( o instanceof Video) ? o.getClass().getName() : "Not matching instance for casting");
It shows Video.
Then that means o is an instance of Video, but the run time cast exception still raise.
I try to place "put" and "get" in the same servlet(put the vector into the servletcontext and get the vector back from the servletcontext), it is okay. quite a strange thing!
Do you encounter this problem?
Yours
Russell
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Welcome to the JavaRanch! Please adjust your displayed name to meet
the JavaRanch Naming Policy. You can change it here.
Thanks!
and welcome to the JavaRanch!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Russell,
u can try one more thing.
do,
ClassLoader oClassLoader = o.getClass().getClassLoader();
Video v = new Video(); // of course if can create ur Video object like this...
ClassLoader vClassLoader = v.getClass().getClassLoader();
//now, print both of these references,
System.out.println("oClassLoader:"+oClassLoader+":");
System.out.println("vClassLoader:"+vClassLoader+":");
if u get both value same then there is some REAL PROBLEM with your webserver.
if not then you HAVE TO HAVE ClassCastException.
i tried above with my code and getting expected results...
again, to remind you,
where you have Video.class? if its in /servlet directory your code code won't work and you will have an exception. put that in the WebServer's STANDARD classpath...whatever it is for you...
regards
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi
when i said "theres REAL PROBLEM WITH YOUR WEB SERVER" that meant you code is "fine'. ur webserver is behaving strangely...
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about the ServletContext
 
Similar Threads
Javaranch mock?Question Bank ID : 44
My BeeServlet won't buzz
404 Error
unable to access LogServlet...not the same error that Juliane had
java.lang.UnsupportedClassVersionError: Bad version number in .class file