1. How do you invoke EJB 3.0 from Servlet and JSP ?

    In Servlet the @EJB annotation or alternatively JNDI look up (old way) can be used to get hold of can be EJB objects. The following code snippet shows how to do it through EJB injection.

    EJB Injection through @EJB

    public class MyEjbServlet extends HttpServlet {
    private HelloUserLocal helloUser;
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      //use the injected resource

    JSP however is not supported to inject. And moreover one should not call/invoke EJB in JSP , since it violates design principles.But if one must then , following code should help : 

    <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,
    foo.HelloWorldLocal" %>
    //declare a "global" reference to an instance of the home interface of the session bean
    HelloWorldLocal helloUser=null;
    public void jspInit()
    //obtain an instance of the home interface
    InitialContext cntxt = new InitialContext( );
    Object ref= cntxt.lookup("java:comp/env/ejb/HelloWorldLocal");
    helloUser = (HelloWorldLocal)PortableRemoteObject.narrow(ref,HelloWorldLocal.class);
    //call the object
  2.  Can we maintain state with a stateless session bean ?

    The answer is NO. Not at least the EJB way. EJB doesn't retain information in stateless session bean. However you can trick the container by using external class serialized with state information and stored in the file system and upon the next call to the  method ( where in you are expecting state of earlier call) you can read the serialized data back.Also you need to make sure the data consistency between client calls and so many other things. With this trick you will end up doing what EJB container does for a stateful beans. If you still feel this trick is a good idea, be my guestsmiley.

  3. Can we have instance fields in Stateless session bean ?

    Yes. A stateless session bean can have instance field, BUT not for storing state information as opposed to Stateful beans. There may be a need of storing certain shareable resources like data source , JMS source etc, which will be used in the exposed method. So it is perfectly safe to have instance fields in the Stateless session beans

  4. Can you inject Stateful beans into stateless session bean or Servlet ?

    Yes!!. No body stops you to inject Stateful beans into stateless session bean or a Servlet. Lets take a Servlet and inject (definitely as a field in Servlet) a Stateful bean; What happens ? Container creates a single instance of a Servlet, so the instance field of Servlet ( which is a Stateful Bean ) injected , is shared among clients.It will lead to drastic data inconsistency ( stored in Stateful bean ) being shared by clients. And this is no different for a Stateful bean injected into stateless bean.So you must AVOID injecting into stateless beans and Servlets. Please don't ask how do you access or make use of Stateful bean in Stateless or Servlet.The answer is JNDI inside the methods of Stateless and doGet() or doPost() of Servlet.

  5. What could be wrong if instead of doing JNDI look up or EJB injection of a Bean named UserRemoteBean I created the instance of the same in the follwing way UserRemoteBean urb=new UserRemoteBean(); and started using it in the client application?
  6. Can you pool stateful session beans ? Justify your answer.
  7. Why can't you have a stateful bean marked as @WebService ?