ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>czp_ejb</display-name>
<enterprise-beans>
<session>
<ejb-name>Advice</ejb-name>
<local-home>com.czp.AdviceHome</local-home>
<local>com.czp.Advice</local>
<ejb-class>com.czp.AdviceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
jboss.xml
<?xml version="1.0"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Advice</ejb-name>
<jndi-name>Advice</jndi-name>
</session>
</enterprise-beans>
</jboss>
web客户端:
jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC
"-//JBoss//DTD Web Application 2.3V2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd">
<jboss-web>
<ejb-local-ref>
<ejb-ref-name>Advice</ejb-ref-name>
<jndi-name>Advice</jndi-name>
</ejb-local-ref>
</jboss-web>
web.xml
<ejb-local-ref>
<ejb-ref-name>Advice</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.czp.AdviceHome</local-home>
<local>com.czp.Advice</local>
<ejb-link>czp_ejb.jar#Advice</ejb-link>
</ejb-local-ref>
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.czp.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
MyServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
InitialContext ctx = new InitialContext();
try{
AdviceHome home = (AdviceHome)ctx.lookup("Advice");
Advice advice = home.create();
advice.sayHello();
}catch(CreateException e){
e.printStackTrace();
}
}catch(NamingException e){
e.printStackTrace();
}
报错语句:
22:56:16,437 ERROR [STDERR] javax.naming.NameNotFoundException: Advice not bound
22:56:16,453 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
22:56:16,453 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
22:56:16,453 ERROR [STDERR] at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
22:56:16,453 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
22:56:16,453 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
22:56:16,453 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)