package ejbmyhello;import javax.ejb.*;/** * HellBean类实现了SessionBean接口 * 在此类中来写我们要实现的业务逻辑 * ---------------------------------------------------------- * 这个企业级Bean中包含了7个文件: * 一个类: * 本类 HellBean * 四个接口: * Hell HellHome HellLocal HellLocalHome * 两个xml文件: * ejb-jar.xml jboss.xml * ---------------------------------------------------------- * */public class HelloBean 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 sayHello(String name) { /**@todo Complete this method*/ System.out.println("欢迎您:"); System.out.println("name"+name); return "我是返回的字符串"; } }
测试客户端代码package ejbmyhello;import javax.naming.*; import javax.rmi.PortableRemoteObject;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class HelolTestClient1 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 HelloTestClient1() { initialize(); } public void initialize() { long startTime = 0; if (logging) { log("Initializing bean access."); startTime = System.currentTimeMillis(); } try { //get naming context //----------------------------------------------------------------------------- //我的代码区 //这个地方你一定要注意 我想你错就应该错在这了........ //---------------------------------------------------------------------------- java.util.Hashtable h = new java.util.Hashtable(); //h.put(Context.PROVIDER_URL,"192.168.0.63");//远程测试 h.put(Context.PROVIDER_URL,"127.0.0.1");//本地测试..... h.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); Context context = new InitialContext(h); //-------------------------------------------------------------------------------- //look up jndi name Object ref = context.lookup("Hell"); //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 bean access through Home interface."); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed initializing bean access."); } e.printStackTrace(); } } //---------------------------------------------------------------------------- // 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(): " + hell + "."); } return hell; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public String sayHello(String name) { String returnValue = ""; if (hell == null) { System.out.println("Error in sayHello(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling sayHello(" + name + ")"); startTime = System.currentTimeMillis(); } try { returnValue = hell.sayHello(name); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: sayHello(" + name + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: sayHello(" + name + ")"); } e.printStackTrace(); } if (logging) { log("Return value from sayHello(" + name + "): " + returnValue + "."); } return returnValue; } public void executeRemoteCallsWithDefaultArguments() { if (hell == null) { System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } sayHello(""); } //---------------------------------------------------------------------------- // 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) { HelloTestClient1 client = new HelloTestClient1(); // 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. client.create(); String str = client.sayHello("章三"); System.out.println("这是客户测试端::name:::"+str); } }
弱弱的问下:这送分有什么用?
给你个例子
public class Test2 {
static Context ctx; public static void main(String[] args) throws NamingException {
ctx = getInitialContext();
Object obj = ctx.lookup("TestJNDI.Test1");
System.out.println("OK");
} private static Context getInitialContext() throws NamingException {
String url = "t3://localhost:80";
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
System.out.println("test begin");
return new InitialContext(h);
}}
5. 部署应用程序
启动Application Dopolyment Tool:新开一个Dos窗口,键入以下命令,%J2EE_HOME%\bin\deploytool 。该工具启动速度可能比较慢,要耐心等待。启动成功后会出现主界面(此时不要关闭Dos窗口)。在该界面中选 择 File菜 单 ,再选New Application项。在 Application File Name 输 入 :C:\HelloWorld\HelloWorld.ear 。在 Application Disply Name 输 入 你所喜欢的显示名如:HelloWorld。点 击 OK,在主界面的树形结构Files-->Applications下将增加新的一项:HelloWorld。这意味着产生了一个新的应用程序。接下来我们要做的就是部署该应用程序。在主界面的树形结构下选中HelloWorld,然后再在主界面的File菜单中选取New-->Enterprise Bean,在弹出的名为“New Enterprise Bean - Introduction”窗口中选取Next跳过第一步,在接下来的一步中,Create New EJB File in Application项中选HelloWorld,在EJB Display Name中填上你喜欢的名字如:Hello World EJB,点击Edit按钮,在弹出的窗口中,Start Directory中填:C:\HelloWorld\,在Available Files中展开树形结构C:\HelloWorld\,选取RemoteInterface.class、RemoteObject.class、RemoteHome.class、Client.class四项,点Add按钮添加,然后按OK确定。此时在Contents框中增加了该四个class。点Next进入下一步。Session项选Stateless,意为不保存session状态。Enterprise Bean Class选RemoteObject。Enterprise Bean Name中填上你喜欢的名字如:Hello World Bean。Remote Home Interface中选RemoteHome,Remote Interface中选RemoteInterface。选Next进入下一步。接下来的步骤可直接点Finish。这时主界面的树形结构中Files-->Application-->Hello World中将出现Hello World EJB-->Hello World Bean子项。在主界面的树形结构下选中Hello World,然后再在主界面的Tools菜单中选取Deploy,将弹出新的窗口名为“Deploy Hello World - Introduction”。Object to deploy中选Hello World,Target server中选localhost,选中Retuen Client Jar,在Client Jar File Name中填上:C:\HelloWorld\HelloWorldClient.jar。
[注:下面的JNDI NAME我找不到。我用的是J2EE 1.4.2。上面的步骤都一样的。]
选Next进入下一步,在Application框的JNDI Name框中双击并填上HelloWorld,注意必须与Client.java中Object obj=initContext.lookup("HelloWorld")的“HelloWorld”保持一致。点Next进入下一步。点Finish完成。这时将出现Deployment Progress窗口。如果有误,该窗口将出现异常信息。如果一切正常,点OK便完成了部署工作。6. 运行应用程序
新开一个Dos窗口。进入C:\HelloWorld\Classes目录下运行:C:\ HelloWorld\Classes>java -classpath %J2EE_HOME%\lib\j2ee.jar;.;HelloWorldClient.jar; Client 。运行成功则出现如下信息:Client Received From Remote Object: "Hello,I'm Remote Object,I received your message: 'Hello,Remote Object!'" 。而服务端Dos窗口(j2ee -verbose)中出现如下信息:Remote Object Received From Client: "Hello,Remote Object!"我照以上的流程运行了客户端。因为我还不是很了解JNDI。大虾们求助阿,好几天了。都快疯了
是不是这样的阿。大家能不能告诉我参数都分别是什么啊?我就是在J2EE下搞的。没有在Weblogic和其它的应用服务器。
小弟感激2楼和3楼的哥们。现在我又试了还不行。我等待中。也郁闷中。
* HellBean类实现了SessionBean接口
* 在此类中来写我们要实现的业务逻辑
* ----------------------------------------------------------
* 这个企业级Bean中包含了7个文件:
* 一个类:
* 本类 HellBean
* 四个接口:
* Hell HellHome HellLocal HellLocalHome
* 两个xml文件:
* ejb-jar.xml jboss.xml
* ----------------------------------------------------------
*
*/public class HelloBean 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 sayHello(String name) {
/**@todo Complete this method*/
System.out.println("欢迎您:");
System.out.println("name"+name);
return "我是返回的字符串";
}
}
import javax.rmi.PortableRemoteObject;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class HelolTestClient1 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 HelloTestClient1() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
//-----------------------------------------------------------------------------
//我的代码区
//这个地方你一定要注意 我想你错就应该错在这了........
//----------------------------------------------------------------------------
java.util.Hashtable h = new java.util.Hashtable();
//h.put(Context.PROVIDER_URL,"192.168.0.63");//远程测试
h.put(Context.PROVIDER_URL,"127.0.0.1");//本地测试.....
h.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); Context context = new InitialContext(h);
//--------------------------------------------------------------------------------
//look up jndi name
Object ref = context.lookup("Hell");
//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 bean access through Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} //----------------------------------------------------------------------------
// 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(): " + hell + ".");
}
return hell;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String sayHello(String name) {
String returnValue = ""; if (hell == null) {
System.out.println("Error in sayHello(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling sayHello(" + name + ")");
startTime = System.currentTimeMillis();
} try {
returnValue = hell.sayHello(name);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: sayHello(" + name + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: sayHello(" + name + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from sayHello(" + name + "): " + returnValue + ".");
}
return returnValue;
} public void executeRemoteCallsWithDefaultArguments() {
if (hell == null) {
System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
return ;
}
sayHello("");
} //----------------------------------------------------------------------------
// 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) {
HelloTestClient1 client = new HelloTestClient1();
// 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.
client.create();
String str = client.sayHello("章三");
System.out.println("这是客户测试端::name:::"+str);
}
}