Implement EJB in a servlet

I googled with no luck trying to understand why Weblogic 10.3.4 does not inject EJB into the annotated field in the servlet.

The ear contains ejb.jar defining the DAO EJB and web.war with TestServlet.

PluginDataDAO.java

@Stateless public class PluginDataDAO implements IPluginDataDAO { } 

IPluginDataDAO.java

 @Local public interface IPluginDataDAO { } 

TestServlet.java

 public class TestServlet extends HttpServlet { @EJB(mappedName = "PluginDataDAO") private IPluginDataDAO pluginDataDAO; } 

web.xml

 <web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>cz.literak.blog.j2ee.TestServlet</servlet-class> </servlet> 

The servlet is inside web.war, EJB in ejb.jar. I tried annotation with / without a mapped name attribute with no luck. When I tried to upgrade web.xml to 3.0, the deployment failed because 3.0 was not listed. What's wrong? Why is pluginDataDAO still null? Thanks.

+7
java dependency-injection servlets ejb weblogic
source share
4 answers

The following combinations work:

servlets

 @EJB private IPluginDataDAO pluginDataDAO; 

web.xml

 ... <ejb-local-ref> <ejb-ref-name>PluginDataDAO</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>cz.literak.blog.j2ee.dao.IPluginDataDAO</local> </ejb-local-ref> ... 

I thought adding links to web.xml is not necessary. What are the rules when to add them?

+5
source share

I had the same problem and it was solved using @ManagedBean :

 @ManagedBean public class TestServlet extends HttpServlet { @EJB(mappedName = "PluginDataDAO") private IPluginDataDAO pluginDataDAO; 
+5
source share

Regarding the issue of Servlet 3; WebLogic 10.3.x is an implementation of Java EE 5, meaning that it only supports Servlet 2.5.

However, the example should work. Perhaps try a completely new project that only has that servlet and EJB.

Also try the same code with the latest WebLogic 12.1.2. It can be downloaded for free on the Oracle website.

+2
source share

I think there is a very good answer in this link ... Embedding Muteless EJB in the servlet ...

this guy Balus says you are trying to use DI in a constructor that is wrong ... earlier you can set it to init () .... just copied the answer, hoping someone else would find it useful

+1
source share

All Articles