File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes JSP as EJB client (Urgent!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP as EJB client (Urgent!)" Watch "JSP as EJB client (Urgent!)" New topic

JSP as EJB client (Urgent!)

Noreen Masterson

Joined: Apr 30, 2002
Posts: 16
Hi there
bit of a problem - i havent used JSP too much before this week - and i am trying to have one as a client to my session bean....except i dont really know how to go about it
can anyone give me some good tips on starting this, or point me to a good tutorial website?
I know how to do the server lookup to locate the home interface of the session but am unsure about setting up initialcontexts etc - would i be better having a bean to do this and then just start on integrating with the session beans?
This has to be done for tomorrow (3pm GMT) so finding out how to do it quickly is of the essence.
many thanx for any help
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
OK, there are several ways to do this. There is the quick way, and there is the (architecturally) correct way.
Being as how you have very little time to do this I suggest the quick way
There is not difference in accessing your EJB from a JSP page, than accessing it from a standalone application (which I assume you have done).
Here are (broadly) the steps:
1) Create you initial context
Context context = new InitialContext();
2) Lookup your bean
Object obj= context.lookup("JNDI NAME");
BeanHome home=(BeanHome)PortableRemoteObject.narrow(obj,BeanHome.class);
3) Create your bean
Bean bean = home.create();
4) Use your bean
5) Remember to catch the relevant exceptions (NamingException, CreateException, RemoteException etc)
And that should be that. To do it in a better way, check out the J2EE Patterns for alternative methods.
Hope this helps

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Noreen Masterson

Joined: Apr 30, 2002
Posts: 16
yes, this looks very much as i thought it would...
however our bean is deployed onto the IBM Websphere Server adn we are using the Webspher commerce suite.
and it seems the jar must be placed into a specific directory -- but i would also assume it needs to be added to a classpath (or some such) that will let the server know to load it....?
any ideas?
I agree. Here's the link:
subject: JSP as EJB client (Urgent!)
It's not a secret anymore!