Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
max zen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephanie Hehreuer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Guess I was wrong. Thanks for clarifying.
 
Camilo Morales
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same here...
 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Valery Pavlov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic