当前位置: 技术问答>java相关
头痛啊!!关于EJB的问题~!
来源: 互联网 发布时间:2017-04-14
本文导语: JBuilder7+WebLogic7,用JSP通过JAVABEAN调用EJB =======================================Enterprise1.java=================== package eb; import javax.ejb.*; import java.util.*; import java.rmi.*; public interface Enterprise1 extends javax.ejb.EJBObject { public Stri...
JBuilder7+WebLogic7,用JSP通过JAVABEAN调用EJB
=======================================Enterprise1.java===================
package eb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Enterprise1 extends javax.ejb.EJBObject {
public String getHello() throws RemoteException;
}
======================================Enterprise1Bean.java================
package eb;
import javax.ejb.*;
public class Enterprise1Bean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
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;
}
public String getHello() {
/**@todo Complete this method*/
return "Hello!Chen!";
}
}
========================================Enterprise1Home.java==============
package eb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Enterprise1Home extends javax.ejb.EJBHome {
public Enterprise1 create() throws CreateException, RemoteException;
}
================================================testejbBean.java==========
package eb;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
import javax.rmi.*;
public class testejbBean {
private String sample = "Start value";
private Enterprise1Home home;
private Enterprise1 e;
//Access sample property
public testejbBean(){
try{
Context Init=new InitialContext();
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");
home=(Enterprise1Home)PortableRemoteObject.narrow(obj,Enterprise1Home.class);
e=home.create();
}
catch(Exception e){
e.printStackTrace();
}
}
public String getSample() {
String tmp="";
try{
tmp=e.getHello();
}
catch(Exception ee){
ee.printStackTrace();
}
if(sample.equals("chenchun"))
return sample;
else
return tmp;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
}
Context Init=new InitialContext();
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");
=====================================
为什么在Javabean里面:
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");总是出错:
javax.naming.NameNotFoundException: Unable to resolve 'app/webapp/JBuilder_DefaultWebApp/23176716/comp/env/ejb/Enterprise1' Resolved: 'app/webapp/JBuilder_DefaultWebApp/23176716/comp/env' Unresolved:'ejb' ; remaining name 'ejb/Enterprise1'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:858)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:223)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:187)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:195)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:195)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:338)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:333)
at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:36)
at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:124)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at eb.testejbBean.(testejbBean.java:16)
at jsp_servlet.__testejb._jspService(__testejb.java:96)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:945)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:332)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:376)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:242)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:5363)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServiceManager.java:721)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3043)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2466)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:152)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:133)
=======================================Enterprise1.java===================
package eb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Enterprise1 extends javax.ejb.EJBObject {
public String getHello() throws RemoteException;
}
======================================Enterprise1Bean.java================
package eb;
import javax.ejb.*;
public class Enterprise1Bean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
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;
}
public String getHello() {
/**@todo Complete this method*/
return "Hello!Chen!";
}
}
========================================Enterprise1Home.java==============
package eb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Enterprise1Home extends javax.ejb.EJBHome {
public Enterprise1 create() throws CreateException, RemoteException;
}
================================================testejbBean.java==========
package eb;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
import javax.rmi.*;
public class testejbBean {
private String sample = "Start value";
private Enterprise1Home home;
private Enterprise1 e;
//Access sample property
public testejbBean(){
try{
Context Init=new InitialContext();
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");
home=(Enterprise1Home)PortableRemoteObject.narrow(obj,Enterprise1Home.class);
e=home.create();
}
catch(Exception e){
e.printStackTrace();
}
}
public String getSample() {
String tmp="";
try{
tmp=e.getHello();
}
catch(Exception ee){
ee.printStackTrace();
}
if(sample.equals("chenchun"))
return sample;
else
return tmp;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
}
Context Init=new InitialContext();
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");
=====================================
为什么在Javabean里面:
Object obj=Init.lookup("java:comp/env/ejb/Enterprise1");总是出错:
javax.naming.NameNotFoundException: Unable to resolve 'app/webapp/JBuilder_DefaultWebApp/23176716/comp/env/ejb/Enterprise1' Resolved: 'app/webapp/JBuilder_DefaultWebApp/23176716/comp/env' Unresolved:'ejb' ; remaining name 'ejb/Enterprise1'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:858)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:223)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:187)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:195)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:195)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:338)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:333)
at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:36)
at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:124)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at eb.testejbBean.(testejbBean.java:16)
at jsp_servlet.__testejb._jspService(__testejb.java:96)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:945)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:332)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:376)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:242)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:5363)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServiceManager.java:721)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3043)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2466)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:152)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:133)
|
Context Init=new InitialContext();
Enterprise1 enterprise1Home Home=Init.lookup("java:comp/env/ejb/Enterprise1");
Enterprise1 obj = enterprise1Home.create();
Enterprise1 enterprise1Home Home=Init.lookup("java:comp/env/ejb/Enterprise1");
Enterprise1 obj = enterprise1Home.create();
|
lookup("java:comp/env/ejb/Enterprise1")中java:comp/env/ejb/Enterprise1是你布置时的BEAN的本地接口的注册名!
to macoff(天堂)用OBJECT是先得到本地接口的引用然后用PortableRemoteObject.narrow方法将其强行转变为本地接口!!
to macoff(天堂)用OBJECT是先得到本地接口的引用然后用PortableRemoteObject.narrow方法将其强行转变为本地接口!!
|
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
Context Init=new InitialContext(properties);
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
Context Init=new InitialContext(properties);