• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating beans with RS

 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm developing a small project using Servlets, Beans and JSP. In the servlet I need to execute a query and populate the Beans with the resultset. Then JSP reads the beans and displays the records. ListBean is bean class. I'm wondering if this is the correct way to populate the beans. If yes, could you please provide me the syntax to read this bean from JSP. Thanks much.


Servlet Code :
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add the arraylist to the session or request object. In the jsp get the arraylist and itterate through the arraylist using the getter methods to get the values out of the bean.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is ok to go with.. I believe that you are not using any frameworks
Use a servlet to set up your object (in this case list) as

request.setAttribute("list", listBean);
RequestDispatcher dispatcher = request.getRequestDispatcher("HelloWorls.jsp");
dispatcher.forward(request, response);

In your JSP page say
ArrayList list = (ArrayList) request.getAttribute("list");
And print out the results in the list

Better programming than this also can be achieved.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use MVC pattern to address this issue.

Create Session.
Check for the bean object in the session.
If no bean object, instantiate and put the bean object in the session.
If the bean object is already in the session, call method to populate the bean with result set.
in the method, store the result set object into collection.
Now you finished filling the bean.
Do RequestDispatcher.forward method to forward to jsp.
In the jsp use "useBean" construct to retrive the information from the bean.

I hope these steps are clear. If you need more assistence let me know
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Rene"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic