Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about the ServletContext

 
vrussell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you setting and retrieving it from the servlet context? Apparently, when you retrieved it, it did not contain a video object.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
when i said "theres REAL PROBLEM WITH YOUR WEB SERVER" that meant you code is "fine'. ur webserver is behaving strangely...
regards
maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic