0

Can not instantiate a bean from simpe cdi webapp.

servlet

@WebServlet(name = "greetingServlet", urlPatterns = {"/sayHello"})
public class GreetingServlet extends HttpServlet {

private static final long serialVersionUID = 2280890757609124481L;

    @Inject
    @Greetings(GreetingType.HELLO)
    private GreetingCard greetingCard;

      public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<h1>" + greetingCard.sayHello() + "</h1>");
      }

}

pom.xml dependencies

<dependencies>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.10.Final</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>    
<version>3.0.1</version>    
<scope>provided</scope>    
</dependency>    

<dependency>    
<groupId>org.glassfish</groupId>    
<artifactId>javax.faces</artifactId>    
<version>2.1.7</version>    
</dependency> 
</dependencies>

I get the error:

SEVERE: Servlet.service() for servlet [greetingServlet] in context with path [/CDI_prog] threw exception
java.lang.NullPointerException
    at com.javacodegeeks.snippets.enterprise.cdibeans.servlet.GreetingServlet.doGet(GreetingServlet.java:37) //`greetingCard.sayHello()`
...

There are no erors in Eclipse projects. I think the problem with dependencies so Tomcat can not detect CDI. Are some dependencies missed?

Community
  • 1
  • 1
furry
  • 66
  • 7
  • Have you followed the instructions at https://docs.jboss.org/weld/reference/1.1.11.Final/en-US/html_single/#d0e5228? Why do you use such an old version of Weld? – JB Nizet Jun 17 '16 at 21:09
  • Agreed, don't use such old versions of libraries. Especially when starting out. They get refined over time. Please also specify what container version you're deploying to. Make sure you are putting all of these dependencies in the war file. – John Ament Jun 17 '16 at 22:48
  • @JBNizet the latest version of Weld solves it, and I did not follow the steps in the link because it works as it is. – furry Jun 18 '16 at 04:45
  • @JohnAment Tomcat 7 used – furry Jun 18 '16 at 04:46

1 Answers1

0

Tomcat 7 doesn't support CDI. You should use JavaEE 6 application container, like Glassfish, Wildfly or something else. It does not matter what do you have in dependencies.

win_wave
  • 1,398
  • 9
  • 9
  • really? I would say that it does support CDI and that happened after I changed weld dependecie to the weld version. – furry Jun 20 '16 at 14:23
  • Servlet is not CDI managed object therefore nothing injected into it. And why it is not managed, because the container is not JavaEE 6 – win_wave Jun 20 '16 at 16:56
  • Maybe you forgot but I used dependencie which enabled injection. – furry Jun 20 '16 at 17:17