JBoss Community

Calling an EJB from servlet

created by Andreas Nauerz in EJB3 - View the full discussion

Hi!

 

I successfully deployed a simple EJB 3.

 

Here is the code:

 

{code}

package ejb;

 

import javax.ejb.Remote;

 

@Remote

public interface HelloWorld {

    public String sayHello();

}

 

 

 

package ejb;

 

import javax.ejb.Stateless;

import ejb.HelloWorld;

 

public @Stateless class HelloWorldBean implements HelloWorld {

    public String sayHello()

    {

        return "Hello world...";

    }

 

}

{/code]

 

 

The deployment tells me about the following bindings:

 

{code}

  java:global/MyEJBProject/HelloWorldBean!ejb.HelloWorld

  java:app/MyEJBProject/HelloWorldBean!ejb.HelloWorld

  java:module/HelloWorldBean!ejb.HelloWorld

  java:jboss/exported/MyEJBProject/HelloWorldBean!ejb.HelloWorld

  java:global/MyEJBProject/HelloWorldBean

  java:app/MyEJBProject/HelloWorldBean

  java:module/HelloWorldBean

{/code}

 

 

Looking up from a standalone client works:

 

{code}

import java.util.Properties;

 

import javax.naming.Context;

import javax.naming.InitialContext;

 

import ejb.HelloWorld;

import ejb.HelloWorldBean;

 

public class Client {

 

    public static void main(String[] args) {

        try {

            Properties env = new Properties();

            env.put(Context.INITIAL_CONTEXT_FACTORY,

                    "org.jboss.naming.remote.client.InitialContextFactory");

            env.put(Context.PROVIDER_URL, "remote://localhost:4447");

            env.put(Context.SECURITY_PRINCIPAL, "xxx");

            env.put(Context.SECURITY_CREDENTIALS, "xxx");

            env.put("jboss.naming.client.ejb.context", true);

           

            Context ctx = new InitialContext(env);

            HelloWorld hello = (HelloWorld) ctx.lookup("MyEJBProject/HelloWorldBean!ejb.HelloWorld");

 

            System.out.println("EJB says: " + hello.sayHello());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

{/code}

 

 

But invocation from a servlet results in error messages due to unsuccessful lookups.

 

I tried:

 

{code}

protected void doGet(HttpServletRequest request,

        HttpServletResponse response) throws ServletException, IOException {

    try {

        response.setContentType("text/plain");

        PrintWriter out = response.getWriter();

 

        Context ctx = new InitialContext();

        HelloWorld hello = (HelloWorld) ctx

                .lookup("MyEJBProject/HelloWorldBean!ejb.HelloWorld");

 

        out.println("EJB says: " + hello.sayHello());

    } catch (Exception e) {

        e.printStackTrace();

    }

 

}

{/code}

 

Any idea?

Reply to this message by going to Community

Start a new discussion in EJB3 at Community