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

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic