Environment used
Netbeans 8.0+
glassfish 4+
jdk 1.8
Note this example works well if you use no interface view .It does not works well if we use local or remote interface
Remote interface
package packt;
import javax.ejb.Remote;
@Remote
public interface SphereBeanRemote {
public double computeVolume(double radius);
}
implementation of above interface
package packt;
import javax.ejb.Stateless;
@Stateless
public class SphereBean implements SphereBeanRemote {
@Override
public double computeVolume(double radius) {
return (4.0/3.0)*Math.PI*(radius*radius*radius);
}
}
client(servlet) which access it
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import packt.SphereBean;
@WebServlet(urlPatterns = {"/SphereServlet"})
public class SphereServlet extends HttpServlet {
@EJB
SphereBean sphere;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SphereServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet SphereServlet at " + request.getContextPath() + "</h1>");
out.println("<h1>" + sphere.computeVolume(18) + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Error HTTP Status 500 - Internal Server Error
type Exception report
messageInternal Server Error
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class SphereServlet
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class SphereServlet
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name=packt.SphereBean,refType=Session into class SphereServlet: Lookup failed for 'java:comp/env/SphereServlet/sphere' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
root cause
javax.naming.NamingException: Lookup failed for 'java:comp/env/SphereServlet/sphere' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name=packt.SphereBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'packt.SphereBean#packt.SphereBean' [Root exception is javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]]
root cause
javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name=packt.SphereBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'packt.SphereBean#packt.SphereBean' [Root exception is javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]
root cause
javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]
root cause
javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs. GlassFish Server Open Source Edition 4.1