0

I'm new in EJB and Java EE. I'm trying to make my first stateless EJB in IntellijIDEA with Tomcat 7. I'm doing it like in Yakov Fein's Java tutorial, but there is he did it in Eclipse\GlassFish. So I'm trying to lookup my bean like

Context ctx = new InitialContext();
HelloWorldBean myBean = (HelloWorldBean) ctx.lookup("java:app/com.practicaljava.lesson32.ejb/HelloWorldBean");

Another way I'm trying ti inject it via @EJB annotation like this

@EJB HelloWorldBean myBean;

But it's not working without in both ways

import com.practicaljava.lesson32.ejb.HelloWorldBean;

I haven't any mapping or settings of EJB in web.xml or another xml with same settings because they aren't exists in that tutorial at all.

The servlet looks like this

@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    //@EJB HelloWorldBean myBean;
    /**
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public HelloWorldServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
     */
    @EJB HelloWorldBean myBean;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // TODO Auto-generated method stub
        try{

        //Context ctx = new InitialContext();
        //HelloWorldBean myBean = (HelloWorldBean) ctx.lookup("java:app/com.practicaljava.lesson32.ejb/HelloWorldBean");

        PrintWriter out = response.getWriter();
       out.println("Now, it's time for dependency injection demo: " + myBean.sayHello());

        }catch (Exception e ) {
            e.getStackTrace();
        }
    }

I was trying to add some libs, make configs, but every time

javax.naming.NameNotFoundException: Name [com.practicaljava.lesson32.client.HelloWorldServlet/myBean] is not bound in this Context. Unable to find [com.practicaljava.lesson32.client.HelloWorldServlet].
Nesquik27
  • 234
  • 1
  • 7
  • 18

0 Answers0