【求救】关于Tomcat中Web客户端,连接JBOSS中EJB3服务端出现的问题
服务端(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
[解决办法]
1,
@Remote ({HelloWorld.class})
@Local ({HelloWorld.class})
2.
props.setProperty("javax.naming.provider.url", "localhost:1099");//如果你是在本机实验,这样写就可以了吧。
3.
刚才看了下,@Remote ({HelloWorld.class}) @Local ({HelloWorld.class})这2个应该只能有一个出现。
[解决办法]
1.启动服务器时使用run.sh -c all -b 192.168.4.3
jboss6文档中有说明,出于安全考虑要绑定IP地址,如果不想绑定就使用0.0.0.0
2.你的jndi有错,应该是ear名+ejb名+方法,在后台部署成功ejb后会有提示这个ejb的jndi地址是多少,比如我的:ejb3demo.ear 有个ejb叫Hello2Bean的,这个jndi就是ejb3demo/Hello2Bean/remote,注意一点要在ear工程的配置文件jboss-app.xml中加上:
<jboss-app>
<loader-repository>
ejb3demo:archive=ejb3demo.ear
</loader-repository>
</jboss-app>
其中ejb3demo:archive就是定义这个ear的名称,祝你好运!