7

I googled without luck trying to understand why Weblogic 10.3.4 does not inject EJB into annoted field in servlet.

Ear contains ejb.jar defining 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>

Servlet is inside web.war, EJB in ejb.jar. I tried the annotation with/without mapped name attribute without luck. When I tried to upgrade web.xml to 3.0, deployment failed that 3.0 is not enumerated. What is wrong? Why is pluginDataDAO still null? Thank you.

Sully
  • 14,672
  • 5
  • 54
  • 79
Leos Literak
  • 8,805
  • 19
  • 81
  • 156

4 Answers4

5

Following combination works:

Servlet

@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 that adding references to web.xml is not neccessary .. What are the rules, when to add them?

Sully
  • 14,672
  • 5
  • 54
  • 79
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
5

I had the same problem and solved it with @ManagedBean:

@ManagedBean
public class TestServlet extends HttpServlet {
    @EJB(mappedName = "PluginDataDAO")
    private IPluginDataDAO pluginDataDAO;
kryger
  • 12,906
  • 8
  • 44
  • 65
user3122855
  • 51
  • 1
  • 2
2

As for the Servlet 3 issue; WebLogic 10.3.x is a Java EE 5 implementation meaning it only supports Servlet 2.5.

The example should work though. Maybe try a completely new project with only that Servlet and EJB in it.

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

Mike Braun
  • 3,729
  • 17
  • 15
  • I did trivial new project. I cannot use newer version as I will use this project for testing on existing setup. But it does not work as expected :-( Do I need to use ejb-local-ref in web.xml? – Leos Literak Aug 20 '13 at 08:36
  • You can use a newer version just for sanity testing. That way you have some more certainty that your code is correct. If it works on 12.1.2 and not on 10.3.4, there may be a bug in 10.3.4. If it also doesn't work on 12.1.2 there's likely another issue in your code that you aren't showing us. – Mike Braun Aug 20 '13 at 08:44
1

I think there is very good answer in this link...Injecting a stateless EJB into Servlet...

this guy Balus is saying you are trying to use DI in constructor which is not correct...earliest you can set it is in init() ....just copied the answer hoping someone else would find usefull

Community
  • 1
  • 1
BatBold
  • 21
  • 1
  • 5