客户端程序如下:
package demo1;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public class Enterprise1TestClient3 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 Enterprise1Home enterprise1Home = null;
private Enterprise1 enterprise1 = null; //Construct the EJB test client
public Enterprise1TestClient3() {
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("Enterprise1");
//look up jndi name and cast to Home interface
enterprise1Home = (Enterprise1Home) PortableRemoteObject.narrow(ref, Enterprise1Home.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access through 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://211.80.197.105: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 Enterprise1 create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
enterprise1 = enterprise1Home.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(): " + enterprise1 + ".");
}
return enterprise1;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String helloworld(String strMsg) {
String returnValue = ""; if (enterprise1 == null) {
System.out.println("Error in helloworld(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling helloworld(" + strMsg + ")");
startTime = System.currentTimeMillis();
} try {
returnValue = enterprise1.helloworld(strMsg);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: helloworld(" + strMsg + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: helloworld(" + strMsg + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from helloworld(" + strMsg + "): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// 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) { // 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.
try {
Enterprise1TestClient3 client = new Enterprise1TestClient3();
Enterprise1 remote=client.create();
System.out.print(remote.helloworld("Demo one11111111"));
}catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
package demo1;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public class Enterprise1TestClient3 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 Enterprise1Home enterprise1Home = null;
private Enterprise1 enterprise1 = null; //Construct the EJB test client
public Enterprise1TestClient3() {
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("Enterprise1");
//look up jndi name and cast to Home interface
enterprise1Home = (Enterprise1Home) PortableRemoteObject.narrow(ref, Enterprise1Home.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access through 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://211.80.197.105: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 Enterprise1 create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
enterprise1 = enterprise1Home.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(): " + enterprise1 + ".");
}
return enterprise1;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String helloworld(String strMsg) {
String returnValue = ""; if (enterprise1 == null) {
System.out.println("Error in helloworld(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling helloworld(" + strMsg + ")");
startTime = System.currentTimeMillis();
} try {
returnValue = enterprise1.helloworld(strMsg);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: helloworld(" + strMsg + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: helloworld(" + strMsg + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from helloworld(" + strMsg + "): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// 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) { // 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.
try {
Enterprise1TestClient3 client = new Enterprise1TestClient3();
Enterprise1 remote=client.create();
System.out.print(remote.helloworld("Demo one11111111"));
}catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
解决方案 »
- resin 启动的时候显示 Unable to access jarfile D:\resin-4.0.22\bin\lib\resin.jar
- ajax向servlet传递参数问题
- 跪求一片关于J2EE外文翻译(要求原文 +翻译文)
- ext中的combo添加expand事件后,如果store为空,则事件失效
- 从oracle数据库读出数据,显示到下拉菜单中,jsp,如何做??
- 推荐初学者学习Java的路线
- 从数据库中读取照片显示在jsp页面中,在tomcat中可以正常显示,为什么在weblogic中是一堆乱码?附源程序,很短
- 对于程序员来说是技术更重要还是业务更重要?
- java中GB2312的16进制编码问题??
- weblogic中能否使用mysql的连接池
- jbuilder7.0与weblogic 7.0出现的错误,请大家指点
- 客户端访问EJB Server出现问题,100分请教
javax.naming.NoInitialContextException: Cannot instantiate class: (无法实例化)weblogic.jndi.WLInitialContextFactory. Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory(找不到类:weblogic.jndi.WLInitialContextFactory)
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)-- Unable to connect to WebLogic server at t3://211.80.197.105:7001
-- Please make sure that the server is running.(请确信服务器已经启动)
-- Failed initializing bean access.(初始化bean操作失败)
我两台机子对接应该也可以呀,以前我rmi程序,对接都可以的,
大家在一台机子上部暑了ejb,
另一台机子访问和在本机访问客户端程序要什么改变吗?
两台机子的安全性要不要设置呢?
还是设置其它配置呢?
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);weblogic.jndi.WLInitialContextFactory需要客户端加载,在weblogic.jar里
是不是也要把weblogic.jar拷到客户机,然后像加j2ee.jar一样加进去?
原来是jndi驱动的问题哦!!!!!!!!!!!!!!!!!!!太开心了!!!!!
请问怎么加载weblogic.jndi.WLInitialContextFactory?你是如何解决的呢?盼赐教!!!