我是Java EE的新手。最近我正在使用无状态bean进行项目,但是出现以下错误
豆 :
@Stateless(mappedName = "FlightServiceBean")
public class FlightServiceBean {
public FlightServiceBean() {
}
// data
}
Servlet:
private FlightServiceBean fs = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("The flights details servlet has been called ...");
try
{
Context context = new InitialContext();
fs = (FlightServiceBean) context.lookup("java:global/ejb1/FlightServiceBean!com.airline.service.FlightServiceBean");
// here where I got the exception
}
catch (NamingException e)
{
System.out.println("Naming Exception has occurred when trying to lookup the flightService EJB");
e.printStackTrace();
}
javax.naming.NamingException:在SerialContext [myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl。中,对于'java:global / ejb1 / FlightServiceBean!com.airline.service.FlightServiceBean''的查找失败。 SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根本例外是javax。 naming.NameNotFoundException:ejb1]
注意:我正在使用glassfish 5.0和jdk 1.8.0
最佳答案
您的EJB查找不正确。尝试将以上行更改为
fs = (FlightServiceBean) ic.lookup("java:comp/env/ejb/FlightServiceBean");
有关EJB查找的更多信息,请参考此link。