I'm developing in Eclipse Juno a project that includes EJB 3.1 and a Dynamic Web Project.
The EJB Class is called FirstBean and the Servlet is called EJB31ServletClient, I have achieved only to make it work in the same server. I have read that I need to set up InitialContext.lookup but almost all examples are made for JavaSE apps.
What I want to achieve:
Deploy the EJB Jar in a Glassfish 3.1.2.2 server and deploy the dynamic web project in a different GlassFish server.
Call in the EJB31ServletClient the sayHello() method.
Here is my code:
The EJB is a very simple Stateless Session Bean with No-interface View that only shows a message:
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@Stateless
@LocalBean
public class FirstBean {
public FirstBean() {
}
public String sayHello() {
return "Hello";
}
}
The Dynamic Web Project has only a Servlet with the following code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.theopentutorials.businesslogic.FirstBean;
@WebServlet("/EJB31ServletClient")
public class EJB31ServletClient extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
FirstBean bean;
public EJB31ServletClient() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(bean.sayHello());
}
}