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 a resultset object from a 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 a resultset object from a servlet to jsp" Watch "Pass a resultset object from a servlet to jsp" New topic

Pass a resultset object from a servlet to jsp

visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122

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

Joined: Mar 22, 2005
Posts: 42965
Have a look at the HttpServletRequest.setAttribute method.
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
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

and in your jsp

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

I hope this is what you are looking for !


SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Visu, you now have 30 posts. Please learn to use the code tags to format your posts.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459


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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

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 ]

[OCA 8 Book] [Blog]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
I agree. Here's the link:
subject: Pass a resultset object from a servlet to jsp
It's not a secret anymore!