public class Servlet2Stateless extends HttpServlet {
@EJB private HelloUserLocal helloUser;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(newSess.getName());
}
will above line of code work when I have EJB and Servlet deployed on different servers? or I need to call it through traditional way????
If the EJB resides on the different server than your client (Servlet) than you cannot use the dependency injection with @EJB annotation.
I guess that you'll need to go with the old JNDI way.
According to EJB 3.1 spec, you can use @EJB annotation in a variety of clients, including servlets which is your case.
The problem is that you are running client and server in different hosts. Depending on the server you are using, you might be able to use the EJB annotation. This post explains how to do it in Weblogic.
Needless to say you have to define the server EJB as @Remote in either case.
If your container also supports CDI, you could write a CDI producer method for the bean whcih does the JNDI lookup. Then you can at least separate the lookup from the injection site.
What about using dependency injection in a standalone client ?