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