aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Question on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Question on "assigning Object  to Context interface"" Watch "Question on "assigning Object  to Context interface"" New topic
Author

Question on "assigning Object to Context interface"

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


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
My Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Freddy,

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.

Please clarify on this.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

To understand more, take a look at this simple example.

Assigning o to a String is legal in Java because String extends Object. Then to get back the String, you cast the o to String again.

Inside the HttpServletRequest.getAttribute() API, it does something like this:

Then to get the Dog object back, you cast it to Dog.


In Java, this is called downcasting. Hopefully it clears your doubt.

[ July 07, 2007: Message edited by: Freddy Wong ]
[ July 07, 2007: Message edited by: Freddy Wong ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Freddy,

Its clearly understood now. I was thinking getAttribute() would return something like {return new Object();} thats why I was confused. Thanks for explaining me the things here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on "assigning Object to Context interface"