I want to use an servlet to calling an sessionBean,what can I do

Discussions

EJB programming & troubleshooting: I want to use an servlet to calling an sessionBean,what can I do

  1. this is my source ,develop is ok.

      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context ctx = null;
        try
        {
          ctx = new InitialContext();
          test1LocalHome test1localhome = (test1LocalHome) ctx.lookup("test1Local");
          String testok = test1localhome.create().getTest();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }

    but this is my exception
    javax.naming.LinkException: . Root exception is javax.naming.NameNotFoundException: Unable to resolve 'app/ejb/test.jar#test1/local-home' Resolved: 'app/ejb' Unresolved:'test.jar#test1' ; remaining name 'test.jar#test1/local-home'

    I use weblogic server,thanks!!!
  2. I think you might have set your initial lookup environment wrong. Double check your JNDI names too (having a JNDI browser might help 'locate' your EJBs if you're not sure how/where to reference them)

    Try this (replace java:comp/env by whatever Weblogic is set to):
                Context initial = new InitialContext();
                Context myEnv = (Context)initial.lookup("java:comp/env");
                Object objYourEJBHome = myEnv.lookup("ejb/YourEJB");
                create...
  3. Your example will work provided that the EJB has been specified via a 'ref' in the deployment descriptor, otherwise, you cannot find it using JNDI ENC.

    Creating an InitialContext with no argument defaults to the host and port on which the servlet is running, but if your EJBs are located elsewhere, then you need to explicitly specify the parameters in the context.

    Raffi