可以呀,我这么实现过其实是一样的查询
就是要初始化Context Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.TengahInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
//指定你的应用服务器的地址
context = new InitialContext(p);
} catch (NamingException ne){
ne.printStackTrace();
throw new ServiceLocatorException("context can't intial");
}以下是一样的查询、调用
就是要初始化Context Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.TengahInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
//指定你的应用服务器的地址
context = new InitialContext(p);
} catch (NamingException ne){
ne.printStackTrace();
throw new ServiceLocatorException("context can't intial");
}以下是一样的查询、调用
解决方案 »
- spring mvc 怎么在后台接收到页面上传来的值
- hadoop单机用例执行异常?
- jsp上传图片到指定目录并保存到数据库的问题!
- 修改web.xml下<welcome-file>无效
- jbpm5.4 变量传值问题 急急急!!!
- weblogic,struts项目,如何指定登录时必须用ssl连接?
- 我学CORE JAVA及其他一些的书入了JAVA的门,下一步该怎么走?谢绝玩笑
- 请问
- 在部署SESSION BEAN时候遇到的问题!
- java 正则表达式里的分组啥意思啊 谁能通熟易懂的举个例子讲下呢
- 讨论一下大家对于Java中进程间通信采取的处理方式
- j2ee中的EAR 和war是怎么生成的,具体用什么工具,怎么生成的
你只需要根据你的SERVER的不同,写相应的Properties
其他的都和以前一样
如果是JBoss用
Context ctx = null;
Properties ht = new Properties();
ht.put(ctx.PROVIDER_URL, "ip:1099" );
ht.put(ctx.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
ctx = new InitialContext(ht);Object objRef = ctx.lookup("Salary");
import java.util.Properties;
import javax.rmi.PortableRemoteObject;public class HelloBeanTestClient1
extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private HelloHome helloHome = null;
private Hello hello = null; //Construct the EJB test client
public HelloBeanTestClient1() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context context = getInitialContext(); //look up jndi name
Object ref = context.lookup("HelloBean");
//look up jndi name and cast to Home interface
helloHome = (HelloHome) PortableRemoteObject.narrow(ref, HelloHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log(
"Succeeded initializing local bean access through Local Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch (Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} private Context getInitialContext() throws Exception {
String url = "t3://ghost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
} return new InitialContext(properties);
}
catch (Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
} //----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//---------------------------------------------------------------------------- public Hello create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
hello = helloHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch (Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(): " + hello + ".");
}
return hello;
} //----------------------------------------------------------------------------
// Utility Methods
//---------------------------------------------------------------------------- private void log(String message) {
if (message == null) {
System.out.println("-- null");
return;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
" ...");
}
else {
System.out.println("-- " + message);
}
} //Main method public static void main(String[] args) {
try {
HelloBeanTestClient1 client = new HelloBeanTestClient1();
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
String out = client.create().hello();
System.out.println(out);
}
catch (Exception e) {
e.printStackTrace();
}
}}
java -classpath ".;weblogic.jar" test.TestClienttest中的文件清单:
testRemote.class 〈〈〈-----EJB远程接口
testRemoteHome.class 〈〈〈-----EJB Home接口
TestClient.java 〈〈〈-----客户程序我用的是weblogic, 所以weblogic.jar要在类路径中