我在尝试执行一个简单的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)
答案 0 :(得分:3)
那就是因为,Tomcat是一个Web容器,而不是一个应用程序容器。 您应该将代码部署在像这样的应用程序服务器中 - Glassfish, JBoss ,TomEE,WebLogic Application Server,WebSphere Application Server等
查看以下链接以获取更多信息
希望这有帮助。
答案 1 :(得分:2)
乍一看,这部分似乎有问题:
@EJB
CurrencyEjb ejb;
您正在尝试执行EJB注入,这在Tomcat中是不可能的。如果您的EJB CurrencyEjb部署在其他地方(即在应用程序服务器中),那么您可以访问它,但不能通过EJB注入。也就是说,Tomcat不是EJB容器,它只是servlet容器,也就是说 - 你不能在它上部署EJB。查看Glassfish,JBoss或TomEE(具有完整Java EE支持的Tomcat扩展)并尝试在那里部署您的项目。
答案 2 :(得分:0)
<servlet>
<servlet-name>CurrencyServlet</servlet-name>
<servlet-class>CurrencyServlet</servlet-class>
</servlet>
可能会发生这种情况,因为您没有设置完整的servlet路径,包括包。 例如:
<servlet-class>your.full.package.CurrencyServlet</servlet-class>