• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

connecting a servlet to a JavaBean

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am stuck
can any one tell me how to connect a servlet to a stateless session bean in jbuilder? or just tell me what I should use? Are there books out there that have made an application to connect a servlet to a javaBean?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one tell me how to connect a servlet to a stateless session bean in jbuilder? or just tell me what I should use? Are there books out there that have made an application to connect a servlet to a javaBean?
I. Yep. Two steps.
- Use IA and 2 if the servlet is in the same container as the EJB.
- Use IB and 2 if the servlet is in a different container from the EJB.

1.A. IF your servlet will run in the same container as the EJB:
Context ctx = new InitialContext();
1.B IF THE EJB is in a different container (say WebLogic):
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "containeradminuserid");
env.put(Context.SECURITY_CREDENTIALS, "containerpassword");
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(env);

2. In either case, get the EJBHome and have Home create the interface:
MySessionEJBHome mySessionEJBHome
= (MySessionEJBHome)ctx.lookup("MySessionEJB");
MySessionEJB mySessionEJB;
mySessionEJB = mySessionEJBHome.create( );
II. The book you want depends a bit on whose Servlet and EJB container(s) you are using. There are also Web-based instructions for different vendors. See:
http://www.javaskyline.com/learnejb.html#vend
Let me know if that helps.
Regards,
Rich
 
Rich Katz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing (or two actually).
1. You need imports for the J2EE classes - especially JNDI, EJB. I think they're:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
You can plug these into JBuilder so you can syntax check.
You need to download a J2EE implementation (such as the Sun J2EE reference implementation) and install it.
http://java.sun.com/j2ee
Be sure to get the right version (1.3 or 1.4) Not all containers support 1.4 yet. Then plug in the jars (add them to the classpath) to your JBuilder project.
2. The other thing - about books. There is the latest Enterprise JavaBeans (3rd Edition) by Richard Monson-Haefel:
http://www.amazon.com/exec/obidos/tg/detail/-/0596002262/
That one and few others are listed at the end of the JavaSkyline Learn EJB page:
http://www.javaskyline.com/learnejb.html#book also.

HTH.
Regards,

Rich
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was a great answer, but I have a slightly different question, to which I hope there is also a great answer. I have some JSPs that share a JavaBean which has session context so that it can save some variables. I would also like to access the bean from a servlet. How would I do that?
 
Jim Martin
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Rich.
 
Sheriff
Posts: 6999
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To fetch a bean from a session context in a servlet, imagine you have declared it in a JSP as:
<jsp:useBean id="beanname" class="com.whatever.MyBean" scope="session"/>
use:
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic