当前位置: 技术问答>java相关
高分求助!j2ee下ejb“调试成功”的例子 (请八年抗战关注)
来源: 互联网 发布时间:2015-03-11
本文导语: | package com.titan.cabin; import java.rmi.RemoteException; public interface Cabin extends javax.ejb.EJBObject { public String getName() throws RemoteException; public String setName() throws RemoteException; } package com.titan.cabin; im...
|
package com.titan.cabin;
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject
{
public String getName() throws RemoteException;
public String setName() throws RemoteException;
}
package com.titan.cabin;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CabinHome extends javax.ejb.EJBHome
{
public Cabin create() throws CreateException, RemoteException;
}
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.SessionBean
{
public String name;
public String getName()
{
return name;
}
public void setName(String str)
{
name = str;
}
public void ejbActivate()
{
}
public void setEntityContext(EntityContext ctx)
{
}
public void unsetEntityContext()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void ejbLoad()
{
}
public void ejbStore()
{
}
public void ejbRemove()
{
}
}
package com.titan.cabin;
import com.titan.cabin.CabinHome;
import com.titan.cabin.Cabin;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.rmi.RemoteException;
import java.util.Properties;
public class Client_1 {
public static void main(String [] args){
try {
Context jndiContext = getInitialContext();
Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHome");
CabinHome home = (CabinHome) javax.rmi.PortableRemoteObject.narrow(obj, CabinHome.class);
Cabin cabin_1 = home.create();
System.out.println("created it!");
cabin_1.setName("Master Suite");
System.out.println(cabin_1.getName());
} catch (java.rmi.RemoteException re){re.printStackTrace();}
catch (javax.naming.NamingException ne){ne.printStackTrace();}
catch (javax.ejb.CreateException ce){ce.printStackTrace();}
catch (javax.ejb.FinderException fe){fe.printStackTrace();}
}
public static Context getInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
// ... Specify the JNDI properties specific to the vendor.
//return new javax.naming.InitialContext(p);
return getJ2EERIInitialContext();
}
public static Context getJ2EERIInitialContext()
throws javax.naming.NamingException {
return new javax.naming.InitialContext();
}
public static Context getGemstoneInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
// Uncomment if you have the com.gemstone.* classes available
//p.put(com.gemstone.naming.Defaults.NAME_SERVICE_HOST,"localhost");
String port = System.getProperty("com.gemstone.naming.NameServicePort",
"10200");
// Uncomment if you have the com.gemstone.* classes available
//p.put(com.gemstone.naming.Defaults.NAME_SERVICE_PORT, port);
p.put(Context.INITIAL_CONTEXT_FACTORY,"com.gemstone.naming.GsCtxFactory");
return new InitialContext(p);
}
public static Context getWeblogicInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.TengahInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
return new javax.naming.InitialContext(p);
}
}
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject
{
public String getName() throws RemoteException;
public String setName() throws RemoteException;
}
package com.titan.cabin;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CabinHome extends javax.ejb.EJBHome
{
public Cabin create() throws CreateException, RemoteException;
}
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.SessionBean
{
public String name;
public String getName()
{
return name;
}
public void setName(String str)
{
name = str;
}
public void ejbActivate()
{
}
public void setEntityContext(EntityContext ctx)
{
}
public void unsetEntityContext()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void ejbLoad()
{
}
public void ejbStore()
{
}
public void ejbRemove()
{
}
}
package com.titan.cabin;
import com.titan.cabin.CabinHome;
import com.titan.cabin.Cabin;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.rmi.RemoteException;
import java.util.Properties;
public class Client_1 {
public static void main(String [] args){
try {
Context jndiContext = getInitialContext();
Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHome");
CabinHome home = (CabinHome) javax.rmi.PortableRemoteObject.narrow(obj, CabinHome.class);
Cabin cabin_1 = home.create();
System.out.println("created it!");
cabin_1.setName("Master Suite");
System.out.println(cabin_1.getName());
} catch (java.rmi.RemoteException re){re.printStackTrace();}
catch (javax.naming.NamingException ne){ne.printStackTrace();}
catch (javax.ejb.CreateException ce){ce.printStackTrace();}
catch (javax.ejb.FinderException fe){fe.printStackTrace();}
}
public static Context getInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
// ... Specify the JNDI properties specific to the vendor.
//return new javax.naming.InitialContext(p);
return getJ2EERIInitialContext();
}
public static Context getJ2EERIInitialContext()
throws javax.naming.NamingException {
return new javax.naming.InitialContext();
}
public static Context getGemstoneInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
// Uncomment if you have the com.gemstone.* classes available
//p.put(com.gemstone.naming.Defaults.NAME_SERVICE_HOST,"localhost");
String port = System.getProperty("com.gemstone.naming.NameServicePort",
"10200");
// Uncomment if you have the com.gemstone.* classes available
//p.put(com.gemstone.naming.Defaults.NAME_SERVICE_PORT, port);
p.put(Context.INITIAL_CONTEXT_FACTORY,"com.gemstone.naming.GsCtxFactory");
return new InitialContext(p);
}
public static Context getWeblogicInitialContext()
throws javax.naming.NamingException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.TengahInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
return new javax.naming.InitialContext(p);
}
}