首先建一个java工程:
EJB接口:
package cn.service;
public interface ServiceTest {
public String sayHello();
}
EJB实现类:
package cn.service.imp;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import cn.service.ServiceTest;
@Stateless
@Remote(ServiceTest.class)
public class ServiceImpTest implements ServiceTest {
public String sayHello() {
return "Hello World";
}
}
JNDI:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
再次:建一个web,导入部置的ejb jar包测试:
<center>
<form action="TestServlet">
<input type="submit" value="submit">
</form>
</center>
servlet代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx = null;
try {
ctx = new InitialContext();
ServiceTest st = (ServiceTest) ctx.lookup("ServiceImpTest/remote");
System.out.println(st.sayHello());
} catch (NamingException e) {
e.printStackTrace();
}
}
报如果下错误,请各位帮看一下:
javax.naming.NameNotFoundException: Name ServiceImpTest is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at cn.servlets.TestServlet.doPost(TestServlet.java:34)
at cn.servlets.TestServlet.doGet(TestServlet.java:20)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
------解决方案--------------------------------------------------------
关键是这一句。
Name ServiceImpTest is not bound in this Context
没有绑定到上下文中。
------解决方案--------------------------------------------------------
EJB没有发布?
------解决方案--------------------------------------------------------
???
------解决方案--------------------------------------------------------