[jboss-user] [EJB3] - Calling an EJB from servlet
Andreas Nauerz
do-not-reply at jboss.com
Fri Oct 26 04:51:46 EDT 2012
Andreas Nauerz [https://community.jboss.org/people/NautilusIII] created the discussion
"Calling an EJB from servlet"
To view the discussion, visit: https://community.jboss.org/message/772525#772525
--------------------------------------------------------------
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
[https://community.jboss.org/message/772525#772525]
Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20121026/16542c00/attachment.html
More information about the jboss-user
mailing list