服务端(test):
HelloWorld接口:
- Java code
package com.test.ejb3; public interface HelloWorld { public String SayHello(String name); }
HelloWorldBean接口实现类:
- Java code
package com.test.ejb3.impl; import com.test.ejb3.HelloWorld; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless @Remote (HelloWorld.class) @Local (HelloWorld.class) public class HelloWorldBean implements HelloWorld { public String SayHello(String name) { return name+":Hello~~!"; } }
客户端(web):
EJBFactory你懂的:
- Java code
package com.web.util; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; public class EJBFactory { public static Object getEJB(String jndipath) { try { Properties props = new Properties(); props.setProperty("javax.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); props.setProperty("javax.naming.provider.url", "jnp://127.0.0.1:1099"); props.setProperty("javax.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); props.setProperty("jnp.disableDiscovery", "true"); InitialContext ctx = new InitialContext(props); return ctx.lookup(jndipath); } catch (NamingException e) { e.printStackTrace(); } return null; } }
index.jsp调用页面:
- Java code
<%@ page contentType="text/html; charset=GBK"%> <%@ page import="com.test.ejb3.HelloWorld,com.web.util.EJBFactory;"%> <% HelloWorld helloWorld = (HelloWorld) EJBFactory.getEJB("HelloWorldBean/remote"); out.println(helloWorld.SayHello("Good")+"<br/>"); %>
在浏览器中打开index.js
出现错误:
- Java code
javax.naming.NameNotFoundException: Name HelloWorldBean is not bound in this Context
我把EJBFactory 的代码改为:
- Java code
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); props.setProperty("java.naming.provider.url", "jnp://127.0.0.1:1099"); props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
在浏览器中打开index.js
出现错误:
- Java code
javax.naming.CommunicationException: Could not obtain connection to any of these urls: 127.0.0.1:1099 [Root exception is javax.naming.CommunicationException: Failed to connect to server 127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server 127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
- HTML code
http://u.115.com/file/f6f9a05f70