call session bean in a servlet


EJB programming & troubleshooting: call session bean in a servlet

  1. call session bean in a servlet (5 messages)

    How do you call a stateless session bean in a servlet code? My code is as follows: Context jndiContext = new InitialContext(); Object ref = jndiContext.lookup("InsertDataBean"); InsertDataRemote idr = (InsertDataRemote) PortableRemoteObject.narrow(ref, InsertDataRemote.class); but it does not find the bean. I use glassfish. Thanks for any help.

    Threaded Messages (5)

  2. Your JNDI connection parameters don't seem to be complete. You need to specify PROVIDER_URL & INITIAL_CONTEXT_FACTORY and InsertDataBeaan's jndi name should be like ejb\InsertDataBean as EJB's are created under ejb node in JNDI Tree. Please check these
  3. did not work[ Go to top ]

    I tried that and now my code looks like response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String email = request.getParameter("email"); String password = request.getParameter("password"); try { Context jndiContext = getInitialContext(); Object ref = jndiContext.lookup("java:comp/env/ejb/InsertDataBean"); InsertDataRemote idr = (InsertDataRemote) PortableRemoteObject.narrow(ref, InsertDataRemote.class); idr.insertUser(username, email, password); } catch (NamingException ex) { Logger.getLogger(Registrator.class.getName()).log(Level.SEVERE, null, ex); out.print(ex.getMessage()); } } public static Context getInitialContext() throws NamingException { Properties properties = new Properties(); properties.put(javax.naming.Context.PROVIDER_URL, "iiop:///"); properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory"); return new InitialContext(properties); } I get error message: No object bound to name java:comp/env/ejb/InsertDataBean I also tried different paths int the lookup method only to get different error message.
  4. If you are using EJB 3.0 specs, then use annotations, then you dont need to do such complex lookups.
  5. another error[ Go to top ]

    Thank you for reply. I am not sure whether this approach worked but at least I got different error message :) javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.RemoteException: null; nested exception is: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'nba_live.sequence' doesn't exist Error Code: 1146 Anyone ever came across such an error?
  6. Re: another error[ Go to top ]

    I had similar problem, though I was looking for local interface, under Glassfish. Might be of use for you. I found two ways of locating the bean using initial context. First is adding this class annotation, defining JNDI name of the bean. @EJB(name = "gatewaysessionbeanref", beanInterface = GatewaySessionLocal.class) then, in code of that same class, use this name for lookup: InitialContext ic = new InitialContext(); GatewaySessionLocal bean = (GatewaySessionLocal) ic.lookup("java:comp/env/gatewaysessionbeanref"); Second, using fully qualified name of bean class, as this: InitialContext ic = new InitialContext(); GatewaySessionLocal bean = (GatewaySessionLocal) ic.lookup("com.mycompany.myproduct.mysubsystem.GatewaySessionBean"); In latter example may be name of local interface class was used, not a bean class, cant tell for sure now. Both these ways work when bean used in pojo class. When trying to use bean from servlet, simple @EJB field annotation works. My servlet and bean located in the same ear. Hope this would help somehow.