The moose likes Servlets and the fly likes Populating beans with RS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Populating beans with RS" Watch "Populating beans with RS" New topic

Populating beans with RS

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360

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 :
Rene Smith

Joined: Jun 10, 2004
Posts: 21
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.
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
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.
dhani dhani

Joined: Nov 04, 2004
Posts: 6
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
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it


Thanks! and welcome to the JavaRanch!


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Populating beans with RS
jQuery in Action, 3rd edition