File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "a question about the ServletContext" Watch "a question about the ServletContext" New topic

a question about the ServletContext


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(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
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)] Source)

Bosun Bello
Ranch Hand

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

So much trouble in the world -- Bob Marley
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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.
Not matching instance for casting
2. i have put TestClass.class in WebServer's classpath
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" ...

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?
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.
and welcome to the JavaRanch!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Russell,
u can try one more thing.
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,
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...
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
when i said "theres REAL PROBLEM WITH YOUR WEB SERVER" that meant you code is "fine'. ur webserver is behaving strangely...
I agree. Here's the link:
subject: a question about the ServletContext
It's not a secret anymore!