在Weblogic7.0下成功地布署了一个CMP,但在调用这个CMP的LOCALHOME时出错!

03-11-02 fengfeng

这个JAR包中有两个Bean, 一个是SessionBean, 一个是EntityBean, 我用一个JSP文件通过SessionBean对EntityBean进行操作,但是在JSP文件中调用SessionBean的LOCALHOME时出现NamingException, 不知哪位高手可以帮助看一下呢!

SignOnLocalHome文件的内容如下:

package com.myweb.ejb.signon;

import javax.ejb.*;
import java.util.*;

public interface SignOnLocalHome extends javax.ejb.EJBLocalHome {
  public SignOnLocal create() throws CreateException;
}

SignOnLocal文件的内容如下:

package com.myweb.ejb.signon;

import javax.ejb.*;
import java.util.*;

public interface SignOnLocal extends javax.ejb.EJBLocalObject {
}

SignOnEJB文件的内容如下:

package com.myweb.ejb.signon;

import javax.ejb.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.myweb.ejb.user.*;

public class SignOnEJB implements SessionBean {
  SessionContext sessionContext;
  private static final String USER_HOME_ENV_NAME = "java:comp/env/ejb/User";
  private InitialContext ic = null;
  private UserLocalHome ulh = null;


  public void ejbCreate() throws CreateException {
    try {
      ic = new InitialContext();
      ulh = (UserLocalHome) ic.lookup(USER_HOME_ENV_NAME);
    } catch (NamingException ne) {
       throw new EJBException("SignOnEJB Got naming exception! " + ne.getMessage());
    }
  }

  public void ejbRemove() {
    /**@todo Complete this method*/
  }
  public void ejbActivate() {
    /**@todo Complete this method*/
  }
  public void ejbPassivate() {
    /**@todo Complete this method*/
  }
  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }
}


UserLocalHome文件的内容如下:

package com.myweb.ejb.user;

import javax.ejb.*;
import java.util.*;

public interface UserLocalHome extends javax.ejb.EJBLocalHome {
  public UserLocal create(int userid, String username, String password) throws CreateException;
  public UserLocal findByPrimaryKey(String username) throws FinderException;
}

UserLocal文件的内容如下:

package com.myweb.ejb.user;

import javax.ejb.*;
import java.util.*;

public interface UserLocal extends javax.ejb.EJBLocalObject {
  public void setUserid(int userid);
  public int getUserid();
  public void setUsername(String username);
  public String getUsername();
  public String getPassword();
  public void setPassword(String password);
}


UserEJB文件的内容如下:

package com.myweb.ejb.user;

import javax.ejb.*;

 abstract public class UserEJB implements EntityBean {
  EntityContext entityContext;
  public java.lang.String ejbCreate(int userid, String username, String password) throws CreateException {
    setUserid(userid);
    setUsername(username);
    setPassword(password);
    return null;

  }
  public void ejbPostCreate(int userid, String username, String password) throws CreateException {
    /**@todo Complete this method*/
  }
  public void ejbRemove() throws RemoveException {
    /**@todo Complete this method*/
  }
  public abstract void setUserid(int userid);
  public abstract void setUsername(java.lang.String username);
  public abstract void setPassword(java.lang.String password);
  public abstract int getUserid();
  public abstract java.lang.String getUsername();
  public abstract java.lang.String getPassword();
  public void ejbLoad() {
    /**@todo Complete this method*/
  }
  public void ejbStore() {
    /**@todo Complete this method*/
  }
  public void ejbActivate() {
    /**@todo Complete this method*/
  }
  public void ejbPassivate() {
    /**@todo Complete this method*/
  }
  public void unsetEntityContext() {
    this.entityContext = null;
  }
  public void setEntityContext(EntityContext entityContext) {
    this.entityContext = entityContext;
  }
 // public abstract void setUserid(int userid);
}

ejb-jar.xml文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
    <enterprise-beans>
        <session>
            <display-name>SignOnEJB</display-name>
            <ejb-name>SignOnEJB</ejb-name>
            <local-home>com.myweb.ejb.signon.SignOnLocalHome</local-home>
            <local>com.myweb.ejb.signon.SignOnLocal</local>
            <ejb-class>com.myweb.ejb.signon.SignOnEJB</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
            <ejb-local-ref>
                <description />
                <ejb-ref-name>ejb/User</ejb-ref-name>
                <ejb-ref-type>Entity</ejb-ref-type>
                <local-home>com.myweb.ejb.user.UserLocalHome</local-home>
                <local>com.myweb.ejb.user.UserLocal</local>
                <ejb-link>UserEJB</ejb-link>
            </ejb-local-ref>
        </session>
        <entity>
            <display-name>UserEJB</display-name>
            <ejb-name>UserEJB</ejb-name>
            <local-home>com.myweb.ejb.user.UserLocalHome</local-home>
            <local>com.myweb.ejb.user.UserLocal</local>
            <ejb-class>com.myweb.ejb.user.UserEJB</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.String</prim-key-class>
            <reentrant>False</reentrant>
            <cmp-version>2.x</cmp-version>
            <abstract-schema-name>UserEJB</abstract-schema-name>
            <cmp-field>
                <field-name>userid</field-name>
            </cmp-field>
            <cmp-field>
                <field-name>username</field-name>
            </cmp-field>
            <cmp-field>
                <field-name>password</field-name>
            </cmp-field>
            <primkey-field>username</primkey-field>
        </entity>
    </enterprise-beans>
    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>UserEJB</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
        <container-transaction>
            <method>
                <ejb-name>SignOnEJB</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor>
</ejb-jar>


weblogic-cmp-rdbms-jar.xml文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd'>
<weblogic-rdbms-jar>
    <weblogic-rdbms-bean>
        <ejb-name>UserEJB</ejb-name>
        <data-source-name>MySQLTxJNDI</data-source-name>
        <table-map>
            <table-name>user</table-name>
            <field-map>
                <cmp-field>password</cmp-field>
                <dbms-column>password</dbms-column>
            </field-map>
            <field-map>
                <cmp-field>username</cmp-field>
                <dbms-column>username</dbms-column>
            </field-map>
            <field-map>
                <cmp-field>userid</cmp-field>
                <dbms-column>userid</dbms-column>
            </field-map>
        </table-map>
    </weblogic-rdbms-bean>
</weblogic-rdbms-jar>


weblogic-ejb-jar.xml文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
    <weblogic-enterprise-bean>
        <ejb-name>SignOnEJB</ejb-name>
        <reference-descriptor>
            <ejb-local-reference-description>
                <ejb-ref-name>ejb/User</ejb-ref-name>
                <jndi-name>UserEJB</jndi-name>
            </ejb-local-reference-description>
        </reference-descriptor>
        <local-jndi-name>SignOnEJBLocal</local-jndi-name>
    </weblogic-enterprise-bean>
    <weblogic-enterprise-bean>
        <ejb-name>UserEJB</ejb-name>
        <entity-descriptor>
            <persistence>
                <persistence-use>
                    <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
                    <type-version>6.0</type-version>
                    <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
                </persistence-use>
            </persistence>
        </entity-descriptor>
        <local-jndi-name>UserEJB</local-jndi-name>
    </weblogic-enterprise-bean>
</weblogic-ejb-jar>

JSP文件的内容如下:

<%!


  public Context getInitialContext(String url) throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY,
          "weblogic.jndi.WLInitialContextFactory");
    p.put(Context.PROVIDER_URL, url);
    return new InitialContext(p);
  }
%>

<%

  String url = "http://" + request.getServerName() + ":" + request.getServerPort();


   try {

  Context ic = getInitialContext(url);

  SignOnLocalHome solh = (SignOnLocalHome) ic.lookup("SignOnEJBLocal");

   } catch (RemoteException ex) {
            
      } catch (CreateException ex) {
            
      } catch (NamingException ex) {
        //主要是这一步出错,每次装载这个页面,这会发生这个例外
            System.out.println("Unable to lookup home: "+ ex.getMessage());
      } 

%>

banq
2003-11-03 13:16
JNDI问题,用一个软件查查你机器的JNDI树,看SignOnEJBLocal是否存在。这个软件可以从sf.net上找到。

fengfeng
2003-11-03 13:36
sourceforge这几天我这里无法访问, 你可否告诉我这个软件叫什么名字,还可以在哪里找到吗.

还在,JNDI我已经在weblogic-ejb-jar.xml哪里定义了,难道还要在其它地方定义的吗!

mep
2003-11-03 19:52
weblogic的console中可以查看JNDI树,在服务器的名字上点击右键,就可以看到JNDI选项了。

fengfeng
2003-11-03 21:57
可以在WEBLOGIC中看到我定义的JNDI名字.

不知哪位高手可以贴出一个最简单的,可以在WEBLOGIC7.0下正常运行的,是通过JSP调用SESSIONBEAN的例子给我参考一下呢!!!

猜你喜欢
2Go 1 2 下一页