This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a Session Bean from a servlet?

 
Priya Sri
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,
Priya Sri.
 
Dave Clark
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

http://www.oreilly.com/catalog/entjbeans4/
http://www.oreilly.com/catalog/jsvrfaces/ and
http://www.oreilly.com/catalog/jvadtaobj/

cheers,

Dave
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
    Regards.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic