This week's book giveaway is in the Testing forum.
We're giving away four copies of Practical Unit Testing with TestNG and Mockito and have Tomek Kaczanowski on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes connecting a servlet to a JavaBean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Reply Bookmark "connecting a servlet to a JavaBean" Watch "connecting a servlet to a JavaBean" New topic
Author

connecting a servlet to a JavaBean

Jim Martin
Ranch Hand

Joined: Oct 31, 2002
Posts: 80
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?
Rich Katz
Greenhorn

Joined: Jul 31, 2001
Posts: 5
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

Joined: Jul 31, 2001
Posts: 5
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
Bill Dornbush
Greenhorn

Joined: Jul 26, 2003
Posts: 10
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

Joined: Oct 31, 2002
Posts: 80
Thank you so much Rich.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
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:


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
 
IntelliJ Java IDE
 
subject: connecting a servlet to a JavaBean
 
Threads others viewed
what is the new mechanism of javabean store
How To Connect a Servlet with an Applet
jsp javaBean problem
web store
How to connect J2ME with mySql
developer file tools