• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin

JavaBeans in Servlets

Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic