1

我在尝试执行一个简单的 ejb/servlet 应用程序时遇到了一些错误。我使用的服务器是使用 eclipse 作为 IDE 的 tomcat 7.0。

这是servlet

public class CurrencyServlet extends HttpServlet {
    @EJB
    CurrencyEjb ejb;
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CurrencyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init(ServletConfig config){

    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    double result=0;    
    String currency=request.getParameter("amount");
    double cur=Double.parseDouble(currency);
    if(cur>0)
    {
        if((request.getParameter("currency").equals("yen")))
                {
            result=ejb.toYen(cur);
                }
        else
            result=ejb.toDollars(cur);
    }
    PrintWriter out=response.getWriter();
    out.println("Your value is worth:"+result);
    out.close();
    }

}


The currencyejb file is as follows.

import javax.ejb.*;
@Stateless
public class CurrencyEjb implements CurrencyMethods{
public double toYen(double request)
{
    double result=request*115.3100;
    return result;
}
@Override
public double toDollars(double request)
{
    double result=request*0.0071;
    return result;
}

}

我的 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>CurrenyConverter</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
        <servlet-name>CurrencyServlet</servlet-name>
        <servlet-class>CurrencyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CurrencyServlet</servlet-name>
        <url-pattern>/CurrencyServlet</url-pattern>
    </servlet-mapping>
</web-app>

我得到的错误如下

javax.servlet.ServletException: Error instantiating servlet class CurrencyServlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)


root cause 

javax.naming.NameNotFoundException: Name CurrencyServlet is not bound in this Context
    org.apache.naming.NamingContext.lookup(NamingContext.java:803)
    org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
4

3 回答 3

3

那是因为,Tomcat 是一个 Web 容器,而不是一个应用程序容器。您应该将代码部署在应用程序服务器中,例如GlassfishJBossTomEEWebLogic Application ServerWebSphere Application Server

查看以下链接了解更多信息

希望这可以帮助。

于 2013-02-16T17:58:56.467 回答
2

乍一看,这部分似乎有问题:

@EJB
CurrencyEjb ejb;

您正在尝试执行 EJB 注入,这在 Tomcat 中是不可能的。如果您的 EJB CurrencyEjb 部署在其他地方(即在应用程序服务器中),那么您可以访问它,但不能通过 EJB 注入。也就是说,Tomcat 不是 EJB 容器,它只是 servlet 容器,也就是说 - 您不能在其上部署 EJB。看看GlassfishJBossTomEE(具有完全 Java EE 支持的 Tomcat 扩展)并尝试在那里部署您的项目。

于 2013-02-16T12:59:06.130 回答
0
<servlet>
       <servlet-name>CurrencyServlet</servlet-name>
       <servlet-class>CurrencyServlet</servlet-class>
</servlet>

这可能是因为您没有设置完整的 servlet 路径,包括包。例如:

<servlet-class>your.full.package.CurrencyServlet</servlet-class>
于 2013-02-16T13:12:12.320 回答