6 Replies Latest reply on Jul 15, 2002 9:31 PM by navneet

    accessing entity beans with servlets

    navneet

      Hi,

      I have an container managed entity bean which implements loachome interface and i want to access this from a servlet.

      i have put the servlet in a WAR file and the entity beans with the interfaces are in a jar file. And both these war and jar are in one EAR file

      My ejb-jar.xml look like the folloing
      ----------------------------------------
      ...
      <ejb-jar>
      <enterprise-beans>

      <display-name>User</display-name>
      <ejb-name>User</ejb-name>
      <local-home>mobile.entity.LocalUserHome</local-home>
      mobile.entity.LocalUser
      <ejb-class>mobile.entity.UserBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>User</abstract-schema-name>
      <cmp-field>
      <field-name>username</field-name>
      </cmp-field>
      .....
      -------------------------------------

      while the web.xml looks the following
      -------------------------------------
      ...
      <web-app>
      <ejb-ref>
      <ejb-ref-name>local/User</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      mobile.entity.LocalUserHome
      mobile.entity.LocalUser
      <ejb-link>User</ejb-link>

      </ejb-ref>

      </web-app>
      -------------------------------------------

      and my code in the servlet is like this:
      ---------------------------------------------
      ....
      Object objRef = ctx.lookup("java:comp/env/ejb/User");
      LocalUserHome home=(LocalUserHome)PortableRemoteObject.narrow(objRef, LocalUserHome.class);
      ....
      ----------------------------------------------

      but this gives me an error

      16:40:06,250 ERROR [STDERR] javax.naming.NameNotFoundException: User not bound
      16:40:06,250 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(Naming
      Server.java:495)
      ....


      Am i doing something wrong. Can i access the entity bean with local interface from the servlet in same EAR. Please help

        • 1. Re: accessing entity beans with servlets
          seven

          I am not sure if u really can do that, I mean access a bean's local interface from an external domain (servlets). I think any bean should have a remote interface in order to be accessed by a client that is calling from outside the jboss.

          • 2. Re: accessing entity beans with servlets

            You need a local ref in your web.xml. Here is an example

            <ejb-local-ref>
            <ejb-ref-name>ejb/local/ClientSupportModel</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <local-home>com.clarity.admin.ClientSupportModelLocalHome</local-home>
            com.clarity.admin.ClientSupportModelLocal
            <ejb-link>ClientSupportModel</ejb-link>
            </ejb-local-ref>


            Access it via: "java:comp/env/ejb/local/ClientSupportModel"

            • 3. Re: accessing entity beans with servlets
              navneet

              Hi jmoring,

              I tried to give the ejb-local-ref in web.xml but it gives be an xml error that ejb-local-ref is not a valid tag.

              I am using the following doctype:

              <!DOCTYPE web-app
              PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
              "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">


              even if you go to the above url you will see that ejb-local-ref is not defined.

              thanks for help anyway!

              • 4. Re: accessing entity beans with servlets
                nhebert

                Navneet,

                From the previous exchanges about your web.xml and its
                DTD being ".../DTD Web Application 2.2..." then perhaps
                you trying to use J2EE 1.2? Are you using JBoss 2.x or
                3.0? This is important as this limits your choices of
                accessing an entity bean efficiently.

                If you are using JBoss 3.0 it is possible to use an
                Entity Bean via its local interface from a co-deployed
                servlet. If you are using JBoss 2.x then, as you have
                discovered, you cannot do that.

                What version of JBoss are you using?

                After knowing that we can help you.

                Cheers,

                Noel.

                • 5. Re: accessing entity beans with servlets
                  nhebert

                  Naveet,

                  I may have been too quick on the reply!

                  Since after reading your post (again) I
                  noticed in your ejb.jar xml that you reference
                  <cmp-version>2.

                  Hmmm, this then indicates that you *may* be using
                  JBoss 3

                  It is a concern that your web.xml DTD on the other hand
                  refernces Servlet 2.2 which is part of J2EE 1.2. This
                  could cause a problem with JBoss 3.0 especially since
                  you said you are 2.2 and you intend to use a 2.3 feature.

                  Just a thought.

                  Cheers,

                  Noel.







                  • 6. Re: accessing entity beans with servlets
                    navneet

                    Hi nhebert,

                    thanks for your help. I think the DTD is the problem. I should have used 2.3. It was a mistake as i copied the DTD from another file which was for earlier version.