File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Pass resultset from servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pass resultset from servlet to jsp" Watch "Pass resultset from servlet to jsp" New topic

Pass resultset from servlet to jsp

michelle cheung

Joined: Jun 05, 2001
Posts: 11
I'm trying to pass a resultset from my servlet to a jsp page.
I'm able to pass Strings, but I can't get the resultset to display on my JSP page.
This is the code in my servlet (I think this line is causing the problems):
results = stmt.executeQuery(sql);
req.setAttribute("pagerrs", results);
This is the code in my jsp:
ResultSet rs = (ResultSet) request.getAttribute("pagerrs");
while( ){
out.println( rs.getString("firstname_txt") );
Nothing prints out, and I don't get an error. And I know that the query does return results.
Any ideas?
Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Rather than trying to figure out this, I'd suggest that you consider creating an object that contains the resultant data and passing that to the presentation layer. All connections, statements and resultset object should be closed in the reverse order that they were opened if you want to avoid stranding valuable resources. You may not see any probelms now, but unless you very careful (and lucky) once you start to scale this to handle large loads you going to run into trouble. A good rule of thumb is to open and close all db resources within the same logical block.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I was going to post something like that last night, but thought I'd double check the API.

Which is not exactly what is happening, but the Statement would definately fall out of scope if you passed the resultset to a JSP.

michelle cheung

Joined: Jun 05, 2001
Posts: 11
Thanks both.
The wierd thing is that I can store the resultset in the session object and everything works as expected.
But I still can't find a way to store it in the request object.
I'm going to try putting the resultset into an array of some sort and see if I can pass that into the request object.
There's no limit on size is there?
k b

Joined: Feb 01, 2001
Posts: 22
How do you transfer control to the JSP from servlet. It should work if you have used forward.
Hope this may help.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Well.. thinking off the top of my head...

Sessions exist and are valid, from one page to the next. That is what makes them sessions. So if you place a resultset in the session, it probably doesn't 'disconnect' itself from the Statement.

But... each and every request object is brand new, and does not maintain state once the page has been served. So in this way, anything that was in there, after the page logic is done, it is gone.

But then again... if it's valid until the moment the page is served.. why would you get a null output? Hmmm... anyone?
I agree. Here's the link:
subject: Pass resultset from servlet to jsp
It's not a secret anymore!