File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes How to call a 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 call a Session Bean from a servlet?" Watch "How to call a Session Bean from a servlet?" New topic

How to call a Session Bean from a servlet?

Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hi friends,
I am a new learner of EJB's. I want to develop an application which is like this..

JSP ---> Servlet----->EJB

Can u please guide me how to call a session bean from a servlet?

Priya Sri.
Dave Clark
Ranch Hand

Joined: Feb 16, 2005
Posts: 52
You'd probably be better off with

Servlet (front Controller) --> Session Bean --> back to Servlet --> forward to JSP

and you'd be better off still if you just use JavaServer Faces rather than Servlets and JSPs alone.

For code examples, it sounds like you need to get yourself a good J2EE book. Try and



Dave Clark<br />Senior WebSphere Architect<br /><a href="" target="_blank" rel="nofollow">Versant Open Access - JDO2 & EJB3</a>
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
This is a very simple task. It looks almost identical with the way any client calls the bean, with couple of differences only:
  • When you build the initial context, you don�t need to set the environment, because your code runs on the server and it is already set.
  • You can "cast" rather then "narrowing" to get a reference to the bean�s local interface.

  • Your code will look like this:

    One more thing: this is the best approach. However you can access your bean through the remote interface, if the bean and the servlet doesn�t share the same classloader.

    I think, therefore I exist -- Rene Descartes
    I agree. Here's the link:
    subject: How to call a Session Bean from a servlet?
    jQuery in Action, 3rd edition