mardi 9 juin 2015

@PersistenceContext not injected in @Stateless which is injected in a servlet

I've the below EJB:

@Stateless
public class ManagementSystem implements ManagementSystemLocal {

    @PersistenceContext(unitName = "Studentms-ejbPU")
    private EntityManager em;

    @Override
    public Users getUser(int userid) {
        return em.find(Users.class, userid);
    }

}

Which is injected in the below servlet:

public class MyServlet extends HttpServlet {

    @EJB (beanInterface=ManagementSystemLocal.class)
    private ManagementSystemLocal ms;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String str1 = request.getParameter("userid");
        Users d = ms.getUser(Integer.parseInt(str1));

        // ...
    }

}

It throws a java.lang.NumberFormatException. In debuger it shows that em is null. How to solve it?

Aucun commentaire:

Enregistrer un commentaire