当前位置: 技术问答>java相关
关于访问EJB环境的问题?在线等待……
来源: 互联网 发布时间:2015-07-23
本文导语: 程序如下: InitialContext ic = new InitialContext(); String dbName = ic.lookup("DataSourceName"); ejb-jar.xml定义如下: …… Data Source of the SessionBean DataSourceName java.lang.String lufengsq...
程序如下:
InitialContext ic = new InitialContext();
String dbName = ic.lookup("DataSourceName");
ejb-jar.xml定义如下:
……
Data Source of the SessionBean
DataSourceName
java.lang.String
lufengsql
……
发生NamingException
InitialContext ic = new InitialContext();
String dbName = ic.lookup("DataSourceName");
ejb-jar.xml定义如下:
……
Data Source of the SessionBean
DataSourceName
java.lang.String
lufengsql
……
发生NamingException
|
String dbName = ic.lookup("DataSourceName");????????
|
to cxjxue
如果你的容器是J2EE RI的话,那么你试试这样
try
{
Context ctx=new InitialContext();
String str=(String)ctx.lookup("java:comp/env/DataSourceName");
}
catch(Exception e){//catch exception}
以上代码是根据你的部署描述符写的。
如果你的容器是J2EE RI的话,那么你试试这样
try
{
Context ctx=new InitialContext();
String str=(String)ctx.lookup("java:comp/env/DataSourceName");
}
catch(Exception e){//catch exception}
以上代码是根据你的部署描述符写的。
|
weblogic 中你可以使用如下代码,这是weblogic的例子代码,
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
Context ctx = getInitialContext();
try {
Object home = (AccountHome) ctx.lookup("java:comp/env/DataSourceName");
return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class);
} catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"containerManaged.AccountHome on the WebLogic server at "+url);
throw ne;
}
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
Context ctx = getInitialContext();
try {
Object home = (AccountHome) ctx.lookup("java:comp/env/DataSourceName");
return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class);
} catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"containerManaged.AccountHome on the WebLogic server at "+url);
throw ne;
}