• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same here...
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic