Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a resultSet from bean to JSP?

Status
Not open for further replies.

squidster

Technical User
Oct 13, 2002
55
GB
Hi,

I'm trying out the MVC method but am a bit stuck with displaying results that require iterating through.

My model bean deals with database connect and query etc but how can I get a result set to be made available to a view JSP, the resultSet is only available in the model bean as the connection is closed as part of the method.

any suggestions?

thanks

 
Store the resultset values in some intermediate object, like an ArrayList, and then cache this object in the HttpSession object. Then, in the JSP page, extract the object from the implicit 'session' object, and then iterate through the ArrayList and display the results.
 
I would like to add a note to the solution given by varocho. Instead of using the HttpSession object to store your data, add it to the request and then do a forward to the jsp page. If you use the HttpSession object too much for some temporary data storage then the session storage will / could get large. The request is flushed after it gets to the jsp page.
 
This may sound stupid but how will I be able to obtain specific data values as the ArrayList or whatever won't know like a ResultSet object so I can no longer use rs.getXXX.

Or am I missing something?
 
You could store values for each column in its own ArrayList object, and then just populate the ArrayList objects with calls to the 'getXXX()' ResultSet functions.

Or, you could create a class with properties that map to the columns in the ResultSet object and use one ArrayList object that contains multiple instances of this new class. The properties of each instance would be set by calls to the 'getXXX()' ResultSet functions.
 
?????

Not that good at java, i'm afraid.

thanks for your help anyway:)
 
In your servlet, do something like:

Code:
java.util.ArrayList column1List = new ArrayList();
java.util.ArrayList column2List = new ArrayList();
...

// Get result set
ResultSet rs = ...

while (rs.next()) {
   column1List.addElement(rs.getString("column1Name"));
   column2List.addElement(new String(rs.getInt("column2Name")));
   ...
}

request.setAttribute("column1Values", column1List);
request.setAttribute("column2Values", column2List);

// Do forward (forward the request and response objects) 
// to JSP
...

Then in your JSP, get the ArrayList objects from the implicit 'request' object.
 
Sorry if i've got this wrong but.....

I have a Control bean that has the doGet and doPost methods which does not query the database at all, this is done by the Model bean. If I include all the necessary code to try as you suggest then this will no longer be MVC?

I can understand the reasons behind MVC but it seems like a real pain in the.....head to actually implement!
 
Looks like fun that I'll have to save till later I guess.

cheers for all your help though!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top