*
The moose likes JSP and the fly likes calling java method in jsp, iterating through results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "calling java method in jsp, iterating through results" Watch "calling java method in jsp, iterating through results" New topic
Author

calling java method in jsp, iterating through results

Jean Jenkins
Greenhorn

Joined: Jul 05, 2008
Posts: 8
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jean Jenkins
Greenhorn

Joined: Jul 05, 2008
Posts: 8
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Jan 10, 2002
Posts: 60785
    
  65

P.S. When posting code, please be sure that it is properly formatted and indented.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calling java method in jsp, iterating through results
 
Similar Threads
com.thinweb.tds.Driver for Ms SQL 7
Problem with c:foreach not working
How to include java utilities beans in my jsp file?
Database access, potential threading problem?
Ljava/lang/String Error Pls Help...