*
The moose likes EJB and other Java EE Technologies and the fly likes How to lookup a EJB 3 session bean from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to lookup a EJB 3 session bean from a servlet?" Watch "How to lookup a EJB 3 session bean from a servlet?" New topic
Author

How to lookup a EJB 3 session bean from a servlet?

max zen
Greenhorn

Joined: Feb 23, 2004
Posts: 5
I get a NullPointException at the save line.

public class Test extends HttpServlet {

private static final long serialVersionUID = -1L;

@EJB
private T2Facade t2fl;

public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

T2 t2 = new T2("hello");

t2fl.save(t2);
LogUtil.log("t2.getId() = "+t2.getId(),Level.INFO,null);
LogUtil.log("t2.getX() = "+t2.getX(),Level.INFO,null);
}


@Stateless
public class T2Facade implements T2FacadeLocal {

@PersistenceContext
private EntityManager em;

public void save(T2 t2) {

//em.persist(t2);
}


@Entity
@SequenceGenerator(name="t2_sequence", sequenceName = "t2_seq")
public class T2 {

private static final long serialVersionUID = -2L;


private Long id;
private String x;

public T2(String x) {

this.x = x;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "t2_sequence")
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hi,

You can use @EJB annotation only inside another EJB. @EJB annotation is ignored by the servlet container. To get EJB instance in servlets you will have to get it from JNDI.

Here's a sample
try {
Context c = new InitialContext();
T2Facade myEJBObject = (T2Facade) c.lookup("java:comp/env/ejb/T2Facade");
} catch(NamingException ne) {
}

Hope this helps
Shailesh Kini


Shailesh Kini.
Stephanie Hehreuer
Greenhorn

Joined: Jul 22, 2007
Posts: 4
Originally posted by Shailesh Kini:
You can use @EJB annotation only inside another EJB.


I don't think that's quite true? You should be able to inject @EJB even into a POJO, so long as it runs in the application client container. Don't know about doing it from within servlets though.
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Hey,

Im not an JEE expert but I think that if you are trying to use a Bean ( no matter if it is from a Servlet or another Bean) in another JVM and another container, you can't use Injections, instead you must use JNDI and lookup for the remote interface of the Bean (Passing the required parameters to the getInitialContext(Properties p) method like the IP address of the container where the Bean is deployed).

What I'm not sure if that if you are in another JVM, you are gonna get a compiler error because I suposse the project where you are building your servlet do not have the definition of the remote interface of the bean.

Amyway, hope this comment helps,

Camilo Morales
SCJP 5


Camilo Morales<br />SCJP 5, SCBCD 5
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
It is true that you cant use Injection in a servlet, you must get the local or remote interface using JNDI.

Camilo Morales
SCJP 5
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Max,

Yes, you absolutely can use @EJB in servlets. In fact, that is the main use case and where EJBs were meant to be invoked from.

I saw someone complain that this was not implemented for some reason, though, in JBoss, or was broken/didn't work. If you are using JBoss that may be what you are seeing. This is definitely not compliant, though. @EJB should work fine in servlets.


-Mike
Pro JPA 2: Mastering the Java Persistence API
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Max,

I just looked at your code and noticed that you are injecting into a type T2Facade, which is the bean impl class. You should define the variable to be of your interface type (T2FacadeLocal) to use the EJB. (I'm actually surprised you are not getting an exception at deployment-time.)
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
All,

Guess I was wrong. Thanks for clarifying.
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Same here...
Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
Mike: you're right, @EJB can't be used in servlets in Jboss 4.2.2. See e.g. the "Getting Started Guide" section 2.1. Thus Jboss is not fully JEE compliant! It took me several days of hair-pulling and wall-kicking to find out.

I have also tried to do a specific JNDI lookup, but have failed so far. Is there any examples of how to do it (preferably portable)?
[ April 01, 2008: Message edited by: Per Lindberg ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10302
    
170

I have also tried to do a specific JNDI lookup, but have failed so far.


Do you see any exceptions? If so post them. Where are you doing the lookup from?


[My Blog] [JavaRanch Journal]
Valery Pavlov
Greenhorn

Joined: Apr 02, 2008
Posts: 1
Yea, i have the same problem with jboss 4.2.2ga.
Also, its call exception, when i try to cast data obtain by using serialization class inside Vector set. 4.2.2 is rake. imho.
Found my way in study EJB 3 with Glassfish v2.1 (its full Java EE 5 supported app.server).
have fun.
Fabian Gutierrez
Greenhorn

Joined: Aug 15, 2004
Posts: 22
you can inject any thing in a servlet, because the container handles its life cycle, however it depends on the spec the web app is using. By now is available from 2.5

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
...
</web-app>

regards,


SCJP 1.4, SCWCD 1.4, SCBCD 5, SCJD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to lookup a EJB 3 session bean from a servlet?