• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass a resultset to a jsp from servlet

 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to pass a ResultSet from a servlet to a JSP in struts 2..
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't using EL be better than scriptlets?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used EL, so if it can do it then its probably better than a scriptlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic