简体   繁体   中英

Use EJB in POJO to apply mvc pattern

I am using EJB 3 + JSP + Servlet. I have read the blog of BalusC. I have one servlet and I wanted to apply creating an ActionFactory to have a flexible mapping of requests.

My Action Interface:

public interface Action {

 public String execute(HttpServletRequest request, HttpServletResponse response) 

throws Exception;

}

public class LoginAction extends UserAction {

    @EJB
    private ProfileManager profileManager;

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        String viewPath = null;
        HttpSession session = request.getSession();

    Map accountMap = profileManager.getAccount(username, password);
}

It throws a NullPointerException and I have read that I cannot inject the @EJB because it only applies to JSF, JSP and servlet. Is it possible to do this kind of pattern with ejb? What would you guys suggest I do instead?

Based on the comments I form my answer. You can do a manual lookup of EJBs through JNDI so you can access them outside of server-managed resources also. Since Java EE 6 the names have been standardized, but you still have to learn the rules on how they are formulated from the Java EE tutorial (or look in the management interface to see under which JNDI names beans are deployed). To do the lookup, do something like this:

InitialContext ic = new InitialContext();
ProfileManager profileManager = (ProfileManager) ic.lookup("java:global/EarName/ModuleName/BeanName")

You'll have to fill in the proper JNDI name yourself. Here is the relevant link to the documentation:

http://docs.oracle.com/javaee/7/tutorial/doc/ejb-intro004.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM