iteye_13917 2010-05-06 00:10
浏览 558
已采纳

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条回答 默认 最新

  • 拽拽的初行者 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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 unity2D 关于圆心产生偏移的问题
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 谁能远程帮我装好软件,破解覆盖主程序,启动后左侧选择字典,输入单词,报酬15元,加我微信15218392686
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件
  • ¥15 gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • ¥15 python使用python-pptx如何给幻灯片添加只读密码。
  • ¥15 深度神经网络传递自变量损失