• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call EJB 3.0 session bean from JSP

 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I tried calling a simple stateless session bean from JSP. I keep getting an error in the JSP saying the "com.simple.ejb" package does not exist. That is it's not able to find my EJB interface class. Could some one please help me in solving this. I can access the session bean from a stand alone java client.

I am using Glassfish as the server. The code is given below.



Any help is very much appreciated.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type of code, e.g. JNDI lookup, shouldn't be in a Java Sever Page.

A better design is to use a Presentation-tier Business Delegate. The JNDI lookup code goes in the delegate class, which handles calls to the EJB.

Java Server Page / Java Servlet call business methods API of Business Delegate. Business Delegate is a simple POJO object.

BusinessDelegate.html
[ October 22, 2008: Message edited by: James Clark ]
 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Thanks for the reply. Certainly I will take your advice. Could you please tell me how do I add the reference to the EJBs in the client project so that I can call the EJB from the web application.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have a Controller in the web application. If so, then the following 4 steps will guide you.

1. Create a class called BusinessDelegate with one or two method signatures of Session EJB's business methods.

2. Implement the methods above using JNDI code to lookup the Session EJB and call the real business methods.

3. In the Controller, add a reference to an instance of BusinessDelegate.

4. From the Controller, based on a user action with a Java Server Page, call the appropriate method on the BusinessDelegate.

Once you have this working, you can then add more methods to the BusinessDelegate class.

Note, that JNDI code is one of the toughest API to get working. You need to make sure that your EJB are deployed and registered in the application server's JNDI namespace. And, you need to make sure that you have the libraries (jar files) in the correct places for your web application.
[ October 22, 2008: Message edited by: James Clark ]
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sivaraman,

Sounds like you don't have the EJB-JAR in your WAR classpath. You will need to mention the EJB-JAR as a module dependency.

Regards,
Reza
 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for all your replies.As Reza correctly pointed out the ejb-jar was not in War classpath. I add the same and wow.. its working...

I wrote my first EJB

James the link you provided is really useful I am following it.

Once again thanks everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic