Instantiate a bean in a servlet
2 posts in topic
Flat View  Flat View

Posted By:   J_Hawley
Posted On:   Wednesday, May 30, 2001 10:17 PM

Is it possible to have a servlet instantiate a bean? I am able to do by doing a out.print(" ")

but all that does is instantiates the bean in jsp. I need to be able to set it in the Servlet.


Re: Instantiate a bean in a servlet

Posted By:   prashanth_vaidyaraj  
Posted On:   Thursday, June 7, 2001 10:26 PM

hi hawley,
to instantiate a bean in the servlet you have to first get the JNDI context using the bean's lookupname, then create a home object and then a remote object of the bean. Once it is instantiated you can call any bean method just as you call any class function. The sample code is given below:

Context ctx=(Context)hc.getInitialContext();

String lookupName = "HawleyHome";

HawleyHome home = (HawleyHome) ctx.lookup(lookupName);
HawleyRemote bean = (HawleyRemote) home.create();

Variable = (Return Type)bean.someFunction();

Hope my answer helps you to solve your problem,


Re: Instantiate a bean in a servlet

Posted By:   John_Zukowski  
Posted On:   Thursday, May 31, 2001 10:31 PM

Just treat it as any other class:

new Hawley.JBean()

Saving stuff in the session is already covered in the servlets FAQ.
About | Sitemap | Contact