While I was going through the Online tutorial on J2EE 1.4, I found the following piece of code, which confused me.
Create an initial naming context. Context initial = new InitialContext(); Context myEnv = (Context)initial.lookup("java:comp/env"); The java:comp/env name is bound to the environment naming context of the ConverterClient component.
My question here is , initial.lookup() method would return an object of type Object class. How could we make a reference variable of type "Context" interface to refer the object of type "Object". I haven't seen anywhere Object implementing Context interface. What kind of polymorphism is that? Or is that anything else. I'm very new to java/J2EE area and hence I'm not sure if I had mistaken that code.
Thanks in advance.
posted 11 years ago
Just an addition to the above question. How do we actually bind the bean to the JNDI. I didn't see any code like ctx.bind(Object,String)? Where do we set this binding?
I haven't seen anywhere Object implementing Context interface.
It's Context that extends Object and not Object implements Context. Every class in Java extends Object (implicitly).
It's actually vendor specific. Since you mentioned about J2EE 1.4 Online Tutorial, you can use deploytool to do that. First, open the EJB JAR, then click on Sun-specific Settings. Map your bean with your JDNI name there.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
posted 11 years ago
Yes I accept that every thing in java extends from Object. Since I believed that, I was not able to understand assigning an object type of Object to a context. Please look below piece of code
Context initial = new InitialContext(); Context myEnv = (Context)initial.lookup("java:comp/env");
when we execute lookup() method, we are sure it returns an object of type Object. How can we make myEnv which is a Context to refer. Also don't we get java.lang.ClassCast Exception when we invoke any method using myEnv.
Also the same query extends to Assume we have an Object Dog, with method wag() and if we say
Dog d=(Dog)req.getAttribute(); //req is a HttpServletRequest d.wag(); //don't we get java.lang.ClassCastException.