gpt4 book ai didi

jsp - 创建简单的 servlet/ejb 程序时出错

转载 作者:行者123 更新时间:2023-11-28 22:05:40 25 4
gpt4 key购买 nike

我在尝试执行一个简单的 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)

最佳答案

那是因为,Tomcat 是一个 Web 容器,而不是应用程序容器。您应该将代码部署在应用程序服务器中,例如- Glassfish , JBoss, TomEE , WebLogic Application Server , WebSphere Application Server等等

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

希望这对您有所帮助。

关于jsp - 创建简单的 servlet/ejb 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com