iteye_13917
2010-05-06 00:10 阅读 478
已采纳

Tomcat上的Web客户端访问EJB服务器报异常,javax.naming.NameNotFoundException,求教!

[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]

郁闷我一天了还没搞定....求指教。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    xiaolongfeixiang 拽拽的初行者 2010-05-06 08:05

    这样试试:

    [code="java"] <%

    Properties props = new Properties();

    props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
    props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
    props.setProperty("java.naming.provider.url","jnp://localhost:1099");

    InitialContext ctx = new InitialContext(props);

    ......
    %>[/code]

    点赞 评论 复制链接分享
  • qwe_rt qwe_rt 2010-05-06 00:27

    [quote]javax.naming.NameNotFoundException: Name UserDaoImpl is not bound in this Context [/quote]

    换种初始化的方式试试:在InitialContext构造函数中传入一个hashtable,
    参考:http://yanghaiskys.iteye.com/blog/223317

    点赞 评论 复制链接分享
  • xiaolongfeixiang 拽拽的初行者 2010-05-06 12:11

    缺少jar包,将相应的jar包放在web-inf的lib目录下

    点赞 评论 复制链接分享
  • qwe_rt qwe_rt 2010-05-06 12:12

    [quote] javax.security.jacc.PolicyContextException [/quote]
    缺少包javaee.jar。

    点赞 评论 复制链接分享
  • xiaolongfeixiang 拽拽的初行者 2010-05-06 12:22

    去掉WEB-INF下的那个jar。试试

    点赞 评论 复制链接分享
  • qwe_rt qwe_rt 2010-05-06 12:28

    [quote]jboss里的 jboss-javaee.jar跟项目自带的jave-ee.jar(新建的是JAVAEE5.0项目)一样,是不是有什么冲突? [/quote]
    肯定有冲突

    只保留javaee.jar里的javax.security.jacc.*就可以了。。

    请看:http://biaowen.iteye.com/blog/344887(含jar包下载)

    点赞 评论 复制链接分享

相关推荐