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

Pass resultset from servlet to jsp

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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( rs.next() ){
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?
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Sean
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
What's a year in metric? Do you know this metric stuff tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic