EJB programming & troubleshooting: Uncomprehendable Code.... HELP ME please

  1. Uncomprehendable Code.... HELP ME please (4 messages)

    While reading a book about accesing a EJB from servlets, I came across this code.
    Can any boby explain me, from layman point of view, the meaning of the code.

    String initCtxFactory = getInitparamter(Context.Initial_context_Factory);

    String ProviderURL = getInitparamter(Context.provider_url);

    // ADD JNDI init paramter to a properties object.

    properties env = new properties();
    evn.put(Context.INITIAL_CONTEXT_FACTORY, initCtxFactory);


    Context ctx = new Initialcontext(env);


    xxxxxHome xxxxxhome = (xxxxxHome)ctx.Lookup("Ecom.xxxxxHome);

    Can we directly use last two lines of codes?
    Can somebody give me a example of code where i can access a session bean and/or entity bean using servlet.?
    thanks in advance
  2. Uncomprehendable Code.... HELP ME please[ Go to top ]

    HI Sam,
             I am pasting here one document extract, It may be helpful to you.

    To create a WebLogic context from a client, your code must minimally specify this factory as the initial context factory, and the URL of a WebLogic Server in the JNDI environment, as properties passed to the constructor of InitialContext. Here's an example of the minimal setup required to establish a context:

       Hashtable env = new Hashtable(5);
       Context ctx = new InitialContext(env);


    You can go to Gopalan site ( I don't have remember url now). There is a good example that how can you access bean from servlet.

    Vivek Gupta
  3. Uncomprehendable Code.... HELP ME please[ Go to top ]

    hello Sam,

    For the servlet to access the Bean, it is first necessary to get the Home Interface of the bean. This is done by querying the JNDI Namespace. Consider the Namespace as a directory which contains the necessary information for locating the home interface of the bean, very much like your local Yellow Pages telephone directory. When the beans are deployed they are registered within a namespace that is specified by the deployer.

    Now for querying the JNDI Namespace you need some kind of a handle. That is the InitialContext structure. Consider the InitialContext as the index of the Yellow Pages telephone directory. Now normally you would use code like the following in your servlet:

    InitialContext ctx = new InitialContext();
    Object objref = ctx.lookup("name of EJB Home Interface");
    BeanHome homebean = (BeanHome) PortableRemoteObject.narrow ( objref, BeanHome.class );

    In the above code the first line will get you the default InitialContext , that which was specified by the deployer. In the case of the code that you saw in the book, instead of using that InitialContext, a different context is used: that new context is specified by the following two parameters:

    1. Context.Initial_Context_Factory
    2. Context.provider_url

    Thus there was a need first to create a properties object, which is nothing but an object version of a config or an ini file. It contains name/value pairs for the configuration information that you might want to pass to a function call. In this case that environment information is passed to the InitialContext call as follows:

    InitialContext ctx = new InitialContext(env);

    Once you get the context the rest of the steps in accessing the Home interface is the same.

    For examples of bean access from a servlet:

    You can check out the Flashline.com site under the BeanQueen section. or

    you can go to the JDC section of the Javasoft site or you
    can read Ed Roman's book Mastering EJBs a copy of which is available for download from TheServerSide.com.


  4. Hi,
    According to Uday he is perfectly correct. The thing is , getting the initial context that depends on the app server you are using. For Web Logic it is different, for Gem Stone it is different. The code which you wrote from that book is specific to a particular application server,Ithink so.
    But what Uday was saying is correct. Actually you have to use PortableRemotebject.narrow() method for EJB1.1 compliant server. For EJB1.0 complaint server you can use just normal java class casting of the class is sufficient.
    If I am wrong please correct me.

  5. PortableRemotebject.narrow() is the way to do it according to EJB 1.1..and so u are correct mate