1

我正在尝试通过 *NIX 上的 Eclipse EE 将 WAR 文件的标准部署过程破译到 Oracle GlassFish 3.1.2。我正在获取java.lang.ClassCastExceptionJNDI 查找字符串(据我所知)。有什么建议么?

JNDI 查找字符串:Object objref = ctx.lookup("java:comp/env/");

SE 上的一些帖子提倡使用:ctx.lookup("java:comp/env/ejb/<mapped name>") So: Object objref = ctx.lookup("java:comp/env/Exchange");`

  • “ejb”是什么意思?它是一个内部的、Java 特定的标识符,还是一个用户区目录?

导航到http://localhost:8080/Exchange/抛出错误:

java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object

堆栈跟踪

[#|2013-02-13T10:26:01.507-0500|WARNING|oracle-glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=73;_ThreadName=Thread-2;|StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object
    at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:245)
    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:153)
    at org.apache.jsp.ExchangeClient_jsp._jspService(ExchangeClient_jsp.java:87)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
|#]

ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>Exchange</ejb-name>
      <home>ejb.ExchangeHome</home>
      <remote>ejb.ExchangeObject</remote>
      <ejb-class>ejb.ExchangeBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>Exchange</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Exchange</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>ExchangeClient.jsp</welcome-file>
  </welcome-file-list>  
</web-app>

ExchangeClient.jsp

<%@page import="ejb.*" %>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.rmi.PortableRemoteObject" %>

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Currency Converter</title>
        <link rel="stylesheet" href="master.css" />
    </head>
    <body>
        <h1>WAR</h1>

        <section id="user_input">
            <form action="ExchangeClient.jsp" method="POST">
                <label>Amount</label>   <input type="text" name="initial_amount" size="16" placeholder="0.00" pattern="^([.0-9])" required/><BR>
                <label>From</label> <input type="text" name="base_trigram" size="16" placeholder="CAD" pattern="^([A-Z])" required/>
                <label>To</label>   <input type="text" name="destination_trigram" size="16" placeholder="USD" pattern="^([A-Z])" required/>

                <button id="submit" type="submit">Submit</button>   
                <button id="reset" type="reset">Clear</button>   
            </form>
        </section>

        <section id="result">
            <%!ExchangeObject exchange; %>
            <%
            String amount = request.getParameter("initial_amount");
            String from = request.getParameter("base_trigram");
            String to = request.getParameter("destination_trigram");

            Context ctx = new InitialContext(); 
            Object objref = ctx.lookup("java:comp/env/");
            ExchangeHome home = (ExchangeHome)PortableRemoteObject.narrow(objref, ExchangeHome.class);

            ExchangeObject exchange = home.create();
            out.println(exchange.convert(amount, from, to));
            out.println(exchange.getExchangeRate());
            out.println(exchange.getExchangeRateTimestamp());
            %>
        </section>
    </body>
</html>

foo@Machine ~/workspace $ tree Exchange
Exchange
├── build
│   └── classes
│       └── ejb
│           ├── ExchangeBean.class
│           ├── ExchangeHome.class
│           └── ExchangeObject.class
├── src
│   └── ejb
│       ├── ExchangeBean.java
│       ├── ExchangeHome.java
│       └── ExchangeObject.java
└── WebContent
    ├── ExchangeClient.jsp
    ├── META-INF
    │   └── MANIFEST.MF
    └── WEB-INF
        ├── lib
        │   └── ejb-jar.xml
        └── web.xml
4

1 回答 1

1

嗨,在您的 EJB 服务器中,您拥有我们所说的 JNDI 服务器。

JNDI 服务器是一个目录。这意味着当您部署 EJB 时,部署者会在 JNDI 树中创建一个条目来引用您的新 EJB(例如 ejb/MyEjb)。

因此,要使用您的 MyEJB,您需要使用其名称“ejb/MyEjb”查找实现:

ctx.lookup("java:comp/env/ejb/ejb/MyEjb")

尝试使用

ctx.lookup("java:comp/env/ejb/Exchange");

如果它不起作用,请尝试浏览您的服务器 jndi 树并替换为 Exchange 的正确路径

有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/jndi/concepts/index.html

问候,

于 2013-02-14T12:39:37.870 回答