users@glassfish.java.net

Help : Servlet can not find EJB using DI in glassfish2

From: <glassfish_at_javadesktop.org>
Date: Sun, 18 May 2008 21:27:20 PDT

Hi all
Please guide me as I am trying a simple example in netbeans6 with glassfish2. I have created a simple stateless Session bean

code:
--------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qc.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author BomanbehramM
 */
@Stateless
public class CountryListSessionBean implements CountryListSessionLocal {
    @PersistenceContext
    private EntityManager em;

    public String getSometingStatic() {
        String s = new String("Something Static");
        return s;
    }

    public List getCountryList() {
         Query q = em.createQuery("select c from country c");
        return q.getResultList();
    }

    public void persist(Object object) {
        em.persist(object);
    }
    
    
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
 
}

--------------------------------------------------------------------------------



with a local interface

code:
--------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qc.ejb;

import java.util.List;
import javax.ejb.Local;

/**
 *
 * @author BomanbehramM
 */
@Local
public interface CountryListSessionLocal {

    String getSometingStatic();

    List getCountryList();

   
    
}
--------------------------------------------------------------------------------



this i am calling from my servlet using Dependency injection


code:
--------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qc.web;

import java.io.*;
import java.net.*;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;
import qc.ejb.CountryListSessionLocal;

/**
 *
 * @author BomanbehramM
 */
public class Ctry extends HttpServlet {
    @EJB
    private CountryListSessionLocal countryListSessionBean;
   
    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Ctry</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Ctry at " + request.getContextPath () + "</h1>");
            String s = countryListSessionBean.getSometingStatic();
            
            out.println();
            
            out.println("</body>");
            out.println("</html>");
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
--------------------------------------------------------------------------------



both the packages can be built fine but when I try to deploy the QC-war package from within netbeans I get the following error


quote:
--------------------------------------------------------------------------------
 
Deploying application in domain failed; Error loading deployment descriptors for module [QC-war] -- Cannot resolve reference Unresolved Ejb-Ref qc.web.Ctry/countryListSessionBean_at_jndi: @null_at_qc.ejb.CountryListSessionLocal_at_Session@null
Deployment error:
The module has not been deployed.
See the server log for details.
at org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:163)
at org.netbeans.modules.j2ee.ant.Deploy.execute(Deploy.java:104)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:277)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:460)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
Caused by: The module has not been deployed.
at org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:157)
... 16 more
BUILD FAILED (total time: 1 second)


--------------------------------------------------------------------------------



Can some one please guide me as to how I may be able to get this working . And tell me what I am doing wrong .
Please help.

Many thanks
Meherdad Bomanbehram
[Message sent by forum member 'meherdadb' (meherdadb)]

http://forums.java.net/jive/thread.jspa?messageID=275098