• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i use EJB in servlets  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have created a EJB project using eclipse successfully deployed into weblogic server
Now i have created Another webApp using eclipse in that i have created Servlet. How can use this EJB in Servlet.



but i am getting Caused By: java.lang.ClassNotFoundException: info.fcs.MyTestEJBLocal
any help

Thanks
Rajendar
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the EJB project added as a dependency to your servlets project? The last time I used EJB's were in 2009 and I remember very vaguely that you have to provide a descriptor file that would tell which EJB to inject. I'm not sure how things changed since then.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Is the EJB project added as a dependency to your servlets project? The last time I used EJB's were in 2009 and I remember very vaguely that you have to provide a descriptor file that would tell which EJB to inject. I'm not sure how things changed since then.


Thanks after adding that error got resolved and end up with

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd advice you to pick up an EJB 3 book and go through the steps of creating and deploying EJB's to Java EE containers.
 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
easiest thing to do is to create a 3rd project, which is Maven EAR project. Have dependencies on both the EJB and the Servlet project so they both get packaged in the EAR. Then deploy the EAR as a single unit to weblogic. From your servlet, you can either use either @EJB MyTestEJBLocal MmyTestEJBLocal; or @Inject MyTestEJBLocal myTestEJBLocal;
 
Akaine Harga
Ranch Hand
Posts: 99
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had the same problem just recently. It's a Weblogic specific problem.

1. If your web app is a part of an EAR and your session bean is there as well just make sure the webapp has the ejb module in its dependency and EAR's application.xml has the ejb module listed. All annotations should work just fine.

2. If you try to access a remote interface of a session bean from another application:

Solution 1: Use a custom service locator looking by the remote facade's global JNDI name (java:global/earname/ejbmodulename/MyFacade!com.package.MyFacadeRemote)

Solution 2: Declare your session bean with mappedName parameter (@Stateless(name = "MyFacade", mappedName = "MyFacade")) making it appear in the root of the JNDI context. And then initialize the remote facade using the same mappedName (@EJB(mappedName = "MyFacade"))


Hope it helps
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!