1

I have a servlet code which calls a ejb stateful session bean code as follows,

public class UsesBeansSF extends HttpServlet {   
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
          // do something
       }

    finally {}
 } 

private SessionBeanSFRemote lookupSessionBeanSFRemote() {
    try {
        Context c = new InitialContext();
        return (SessionBeanSFRemote) c.lookup("java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

}

This code works well without the line between * marks. However, when I am adding SessionBeanSFRemote sessionBeanSF = lookupSessionBeanSFRemote() this line (means calling a Stateful Session Bean), the code is giving error. Actually, I have to call the stateless session bean in order to perform some job. Can anybody help me why it is happening ? Thanks in advance.

Error message is following:

type Exception report message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: PWC1392: Error instantiating servlet class websSF.comsSF.UsesBeansSF

root cause

com.sun.enterprise.container.common.spi.util.InjectionException: 
         Error creating managed object for class websSF.comsSF.UsesBeansSF

root cause

java.lang.reflect.InvocationTargetException

root cause

java.lang.RuntimeException: javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]]

root cause

javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote
[Root exception is java.lang.NullPointerException]]

root cause

javax.naming.NamingException: ejb ref resolution error for remote business 
interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]

root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.

Server: GlassFish Server Open Source Edition 3.0.1

alessandro
  • 1,681
  • 10
  • 33
  • 54
  • 1
    You need to tell us the errors that you get. We are not fortune-tellers :P – Mr.J4mes Jan 01 '12 at 11:12
  • 1
    Is the bean stateful or stateless? If it's stateful, you should lookup a new instance at each request, and not share a unique instance among all requests. – JB Nizet Jan 01 '12 at 11:19
  • @JB Nizet, the bean is Stateful. By the way, can you explain it little bit more, means what I have to do exactly. – alessandro Jan 01 '12 at 11:24
  • 1
    A stateful bean is used to maintain a conversational state with 1 user. You lookup a stateful bean and store in a servlet field. A servlet is a singleton: the same instance if used concurrently by all the users of the application. You should declare the bean and look it up in the processRequest method (and potentially store it in the HTTP session). – JB Nizet Jan 01 '12 at 11:35
  • Thanks. By the way, you are trying to mean the code like the updated code of my question ? – alessandro Jan 01 '12 at 12:01
  • Yes, except the field declaration and initialization should be removed (the line between *). BTW, of you change your question each time you get an answer or comment, the answers and comments won't make much sense anymore. – JB Nizet Jan 01 '12 at 12:03

1 Answers1

3

I'm not sure if you have properly set up your Stateful bean. You can try this:

@Stateful(mappedName = "ejb/myStatefulBean")
public class MyStatefulBean implements MyStatefulRemoteInterface {
   // Your implementation
}

then you can look it up with:

InitialContext context = new InitialContext();
MyStatefulRemoteInterface myStatefulBean = (MyStatefulRemoteInterface) context.lookup("ejb/myStatefulBean");

Besides, this stateful bean should be saved into each client's session for re-using:

HttpSession clientSession = request.getSession(false);
clientSession.setAttribute("myStatefulBean", myStatefulBean);

In future requests, you can try to get the bean from the client' session first before creating a new one for him.

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Thanks. By the way, you are trying to mean the code like the updated code of my question ? – alessandro Jan 01 '12 at 12:01
  • No, it's obviously not what he's suggesting. Your exception is caused by an incorrect name passed to lookup(). You didn't change the name at all. – JB Nizet Jan 01 '12 at 12:06
  • The "remote" suggest a remote interface is used, but the usage pattern suggests local beans are used. Also, you don't need the mappedName. – Mike Braun Jan 01 '12 at 17:21
  • @MikeBraun: My example is simply an example on how to look up a Stateful bean. I am not suggesting that he should or should not use Remote interface. Besides, `mappedName` is definitely required. It's the parameter for the `lookup` method. – Mr.J4mes Jan 01 '12 at 17:45
  • @Mr.J4mes why do you think mappedName is required and why do you think it would be needed in the lookup method? – Mike Braun Jan 01 '12 at 19:34
  • @MikeBraun using `mappedName` is a very easy way to declare an EJB for looking up with the `lookup` method. If you don't do anything at all, please tell me how you can look up a bean? What do you think is needed to look up a bean with JNDI? – Mr.J4mes Jan 01 '12 at 19:55
  • @Mr.J4mes mappedName is a relic from the past. Every EJB is assigned a portable JNDI name (see e.g. the wikipedia article about EJB). Just use that. No usage of mappedName is ever portable. – Mike Braun Jan 01 '12 at 20:06
  • @MikeBraun :) I have always had troubles with global JNDI name. Do you mind making another answer explaining steps by steps how you can do it with portable JNDI name? – Mr.J4mes Jan 01 '12 at 20:13