aspose file tools*
The moose likes Struts and the fly likes Pass a resultset to a jsp from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Pass a resultset to a jsp from servlet" Watch "Pass a resultset to a jsp from servlet" New topic
Author

Pass a resultset to a jsp from servlet

rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
How to pass a ResultSet from a servlet to a JSP in struts 2..
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Create an object that maps to the columns in your resultset and then pass that object or a collection of that object to the JSP. You should not be referencing a resultset directly from the JSP.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
This is how I am coding my servlet stuff ...

ArrayList agencyList = new ArrayList();
agency.setLngTransactionId(rSet.getInt("AGCY_TAX_ID"));
agency.setFltCommissionPercentage(rSet.getInt("AGCY_COMSN_PCT"));
agencyList.add(agency);

after this how do I pass this arraylist object to JSP.. Do I need to use any setAttribute functions or something else...
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30894
    
158

Originally posted by rahulJ james:
Do I need to use any setAttribute functions or something else...

Yes. request.setAttribute() will do it. (or session.setAttribute() if you need the data on multiple pages.) Then you can use bean:define in the JSP to grab a reference to the list.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
If you're using struts2 I don't think there is a tag that will give you the ability to get values directly from the request object like you can in struts1. I also don't think that request attributes and parameters get put onto the value stack automatically (if they do I haven't found the correct syntax with which to access them) so you may need to put a scriptlet in your JSP to get the data from your request. If you want to access the values from STRUTS tags with OGNL you'll need to push the value onto the stack.

You could also store the arraylist in your action class and create setters and getters for it. Then your JSP will have access to it and it will be on your OGNL stack automatically.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30894
    
158

Wouldn't using EL be better than scriptlets?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Originally posted by rahulJ james:
How to pass a ResultSet from a servlet to a JSP in struts 2..


With Struts 2 you can simply provide a public getter method for the result set and use a combination of the <s :p roperty...> tag and OGNL in the JSP.

That said, the previous response that suggested creating a bridge between the result set and the JSP page is a cleaner solution; rather than having to use (pseudo-JSP):



you can say:



Or, if you don't mind mixing paradigms:


[ October 04, 2008: Message edited by: David Newton ]
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I haven't used EL, so if it can do it then its probably better than a scriptlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass a resultset to a jsp from servlet