[color=red][b]服务器端:[/b][/color]
[code="java"]
@Stateless
@Remote({UserDao.class})
public class UserDaoImpl implements UserDao {
/**
* unitName="ejb" 是调用persistence.xml中的ejb这个数据源
* 如果persistence.xml里只有一个数据源配置,则无需显示调用unitName="ejb"
*/
@PersistenceContext(unitName="ejb")
private EntityManager em;
@SuppressWarnings("unchecked")
public List<User> getAll(){
Query query = em.createQuery("from User u order by u.id desc");
List<User> users = query.setMaxResults(100).setFirstResult(0).getResultList();
em.clear();
System.out.println("getAll has been called!! ! ");
return users;
}
[/code]
服务器启动后,可以看到控制台提示UserDaoImpl/remote已经绑定到服务器。
[color=red][b]客户端端1:[/b][/color]
普通的JAVA类进行客户端访问
[code="java"]
public class EJBClient {
public static void main(String[] args) throws Exception{
try{
//默认情况下去类路径src下寻找jndi.properties,也可以显示的写出来
InitialContext ctx = new InitialContext();
UserDao userDao=(UserDao)ctx.lookup("UserDaoImpl/remote");
List<User> users = userDao.getAll();
System.out.println(users.get(0).getUsername());
}catch(Exception e){
e.printStackTrace();
}
}
}
[/code]
在src下放置jndi.properties:
[code="java"]
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
[/code]
[color=red]此时,客户端可以正常访问远程方法![/color]
[color=red][b]客户端端2:[/b][/color]
新建一个web项目,把jboss中的client包里的所有jar包都拷贝到项目中。
新建一个JSP页面,将客户端1中的代码复制进去
index.jsp:
[code="java"]
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="javax.naming.InitialContext,java.util.*,dao.UserDao,entity.User"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
My JSP 'index.jsp' starting page
<%
InitialContext ctx = new InitialContext();
System.out.println(ctx.getEnvironment());
UserDao userDao=(UserDao)ctx.lookup("UserDaoImpl/remote");
List users = userDao.getAll();
System.out.println(users.get(0).getUsername());
%>
测试servlet
[/code]
同样在src下放jndi.properties进去
[color=red]此时却出现问题了![/color]
[code="java"]
2010-5-6 0:06:45 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
javax.naming.NameNotFoundException: Name UserDaoImpl 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 org.apache.jsp.index_jsp._jspService(index_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
[/code]
郁闷我一天了还没搞定....求指教。