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

JavaBeans in Servlets

Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I'm working on an application that consists of jsp's a servlet and some javabeans. The servlet is going to query a database for information that I need to store in a jsp page. I figure the best way to do this would be to have the servlet create a bean and store all the information from the database in it, and then have the mainjsp access the bean. The mainjsp will change as users update it randomly. My question is how do I initialize a bean in the servlet and put the database info into it and which scope would be best for this scenario?
I figured I could just create an instance of the Bean and then use it's set method's to set the information, but then how would i define the scope of the bean?
Thanks for any help.
[This message has been edited by Sean Casey (edited September 05, 2001).]
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
Hello Sean
What you are talking is the MVC architecture. Where the JSP is the View , Servlets act as Controller and the Beans as Models
... Let the Servlet instantiate the bean by creating the object of the bean class and use the setter methods for setting different bean properties.
to pass the same information to the next jsp page ( used to display the properties ) you have two options either bind the bean object to the session object or to the request object.
If you bind to the Session .. get the session in the next Jsp page and use <yoursessionvariable>.getValue("<beanobject>")
if you are biding to the request object then use request Dispatcher to forward control to the next JSP page so that the request object is forwarded to the next page. and you can retrive that same object on the next page...
Then on the JSP page use the getter methods .. which will contain the output you want
IBM Certified WebSphere Application Server V3.5 Specialist

regards,<br />Dharmesh Chheda
I agree. Here's the link:
subject: JavaBeans in Servlets
It's not a secret anymore!