Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling java method in jsp, iterating through results

 
Jean Jenkins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to java and jsp. I'm trying to write a program that queries a database, stores the data in a string and outputs the results in a select(drop) box.

I've written my class for the watch. I can't figure out how to retrieve the strings and then iterate through them to display the results as option values in my box. I'm trying to use a java bean.

Here is my code for the class:


I'm pretty sure there are errors in this code; I'm still trying to figure it out. I also don't understand how to iterate through the results once I'm in the jsp so they display in my select box. Thank you in advance for any help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A string isn't going to be very useful in building a set of option elements. Rather you want to store each individual element in a List or Map (depending upon whether your option value and display text are going to be the same or not).

You can then call your method from the page controller and pass the list (or map) to the JSP as a scoped variable.

On the JSP, you will then use the JSTL and EL to loop over the list (or map) and construct the option elements.

Step 1 is to get the method correctly returning the collection of individual values that the represent the options.
 
Jean Jenkins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Thanks for your reply. I think I understand what you mean about using a list - store everything and then retrieve it. I really have no idea about how to retrieve it and then iterate through it. I think I can construct the iterator:


I'm assuming this goes in my jsp where the select box will be.



I still don't understand how to call this method in my jsp and then iterate through the list to ouput the values in option tags. Should I use my bean in my call as in mybean.getWatches()?

[ July 22, 2008: Message edited by: Jean Jenkins ]

[ July 22, 2008: Message edited by: Jean Jenkins ]

[ July 22, 2008: Message edited by: Jean Jenkins ]
[ July 22, 2008: Message edited by: Jean Jenkins ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't be calling the method in your JSP -- that's really bad practice. Rather call the method in the page controller and place the returned list onto the request as a scoped variable. The JSP then accesses this variable using the JSTL <c:forEach> action in order to iterate over it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. When posting code, please be sure that it is properly formatted and indented.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic