简体   繁体   中英

How to call EJB 3.0 by external Servlet

I am learning EJB 3 and trying some simple stuff on it.

I am using eclipse -> Luna Service Release 1 (4.4.1) with JBOSS AS 7.1.1 (final)

I created a simple EJB project in eclipse with name -> SalutationEJBProj. Under this project created a simple session, stateless Bean.

code:

package com.vipin.bean.session.stateless;

import javax.ejb.Stateless;

@Stateless(mappedName="SalutationBean")
public class SalutationBean implements SalutationBeanRemote {

public SalutationBean() {

}

@Override
public String getFormalSalutation(String name) {

    return "Dear" + name;
}

@Override
public String getInfomalSalutation(String name) {

    return "Hi" + name;
}

}

The Remote interface is:

package com.vipin.bean.session.stateless;

import javax.ejb.Remote;

@Remote
public interface SalutationBeanRemote {

public String getFormalSalutation(String name);

public String getInfomalSalutation(String name);
}

I am able to deploy it in Jboss AS 7.1.1 and here is the JNDI bindings output when i start the jboss:

    java:global/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:app/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:module/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemot
java:jboss/exported/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:global/SalutationEJBProj/SalutationBean
java:app/SalutationEJBProj/SalutationBean
java:module/SalutationBean

Now, i want this ejb to be accessible by an external Servlet, for this I created a dynamic web project with name --> SalutationServletProj

Here is the code of the simple Servlet:

package com.vipin.servlet;

@WebServlet(urlPatterns = { "/SalutationServlet" })
public class SalutationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@EJB
private SalutationBean salutationBean;  // <-- How will this be accessible, this is in a different project, how do we import this?

public SalutationServlet() {
    super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

My intention is to use @EJB annotation (or any other method) to get the instance of the EJB in the Servlet.

As these two are in different projects, how can i achieve this? I can't use like this in servlet:

import com.vipin.bean.session.stateless.SalutationBean

1) What are the files from EJB project that i have to copy into servlet project.

2) Will it work as @EJB annotation or do i need to do JNDI lookup? (Because the servlet is remote).

3) Where can i find the classes/libraries generated in jboss?

Any help highly appreciated.

it should be sufficient to only add the SalutationBeanRemote into WEB-INF/classes of your web project.

Then it should work out if you add the following member to your Servlet:

@EJB(lookup="java:global/SalutationEJBProj/SalutationBean")
private SalutationBeanRemote salutationBean;

Only the java:global... names will work because your servlet is running in another module and application as the EJB. I am unsure if the java:jboss/exported... lookup name works or if it is only for remote clients.

Best regards, Robert

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