File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass resultset from servlet to jsp

 
michelle cheung
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic