• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating beans with RS

 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • 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 :
 
Rene Smith
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
dhani dhani
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic