Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass a resultset object from a servlet to jsp

 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Using this code i attained a resultset object which i want to pass it to the jsp page for display.How to pass the object? Can anyone help me out?
Thanx in advance.
Visu N

[Edit - code tags added, broke up long lines]
[ December 16, 2005: Message edited by: David O'Meara ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the HttpServletRequest.setAttribute method.
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wont recommend your standards !! I think it will be good if you seperate your application into different layers. You can have an action class, business layer, DAO layer and make use of VO objects to pass data between layers. Just a suggestion

To pass your resultset to jsp, make use of the HttpServletRequest object

in your servlet
request.setAttribute("resultset",rs);

and in your jsp

ResultSet rs = (ResultSet) request.getAttribute("resultset");

I hope this is what you are looking for !

Thanks,
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visu, you now have 30 posts. Please learn to use the code tags to format your posts.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.setAttribute("resultset",rs);[/QB]


I disagree completely, do NOT pass the resiult set from the Servlet to the JSP. This is a dangerous practice since it can cause memory leaks since you will not be able to close the connection, and ResultSet objects are not serialisable so you will have problems in some containers.

You need to perform the query, load the database results into something else, then send these results to the JSP for display. This is where ORM layers such as Hibernate, Toplink etc come in so handy.

Also, please look at using PreparedStatements, they will also save you a lot of trouble in the future.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I concur, result sets are not serializable and therefore should never be passed over any transport level. You an use auto-generation tool or IDE (like Eclipse) to create a simple DTO object with getters/setters and send as that, or an array of them.

On top of that, given you can vary the fetch size of the result set, I'm not even sure it would work without throwing serious errors.
[ December 16, 2005: Message edited by: Scott Selikoff ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet object becomes invalid/closed after closing its statement.

The best practice is to release DB resources as soon as you are finished with those to avoid sticking with database, just for nothing.

Docs: A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic