File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Display a list based on JSP input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Display a list based on JSP input" Watch "Display a list based on JSP input" New topic

Display a list based on JSP input

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1130
If you enter a text string in an HTML TEXT control and you then want to go to another JSP page and dymamically create/populate some values in a "list" of some kind, ***based on what the user entered on the last JSP page***, is this HTML, DHTML, or something else?
I'm not asking about "how" to get the data. Assume that in the second JSP page, I already have the data, but need to populate some kind of "picker" control, on the fly, that the user can use to choose the fields (values) he wants.
I'm not sure how to create a list on the fly.
I am using a bean to return the database results, and that part works fine.
I appreciate anyone's input on this.
-- Mike
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

First page selects cola manufacturer
Second page lists beverages made by the selected manufacturer.

You've already got the dynamic list of drinks, right? You told us to assume you had the data already.

If you already have the beverage list, then loop over the data, outputting checkboxes of the same name, but with different values (for example, the primary key of each drink). Another control you could use is the select control with multiple="multiple" attribute set. The loop would then be producing multiple option tags with different values.

Once this is submitted, it would be an array with the name of the checkboxes (or the select control), and would contain the primary key of only those drinks picked by the user from the list provided.

last little bit: If you 'already have the data', but that data is not already 'filtered' based on what the user had previously selected... so in my example, the user selects "pepsico international", but the list of drinks on the next page contains drinks by coca-cola, then I'd suggest re-writing your SQL query. Your query should return results based on the first selection.
[ July 25, 2002: Message edited by: Mike Curwen ]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1130
Sorry I haven't been more clear.
Yes, the SQL works fine and correctly as you indicated it should. I wasn't focusing on the SQL.
My issue is how do you populate a HTML list at runtime? Is this just a JSP loop where you populate some HTML control?
Justin F

Joined: Jul 23, 2002
Posts: 4
Perhaps Something like this:
<%@ page import="java.sql.*" %>
<th>cata 1</th>
<th>cata 2</th>
Connection conn = null;
ResultSet rs = null;
Statement st = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/myDB");
st =conn.createStatement();
rs = st.executeQuery("select * from myTable");
while( {
<%= rs.getString("myElement") %></td>
<%= rs.getString("myOtherElement") %>
catch (Exception myEx) {
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
sounds like you're looking for a select list, in html the form it tales is:

IF you want the client to be able to select mopre than one option at a time then use the multiple option in the tag:
<select name="name" multiple>
When the user selects options and submits the form they'll come to the JSP or servlet as parameters of the HttpRequest.
so to dynamically create one, just get your data, create the initial tag, then loop through your data and generate the option tags, then close the switch tag.
hope that helps

I agree. Here's the link:
subject: Display a list based on JSP input
It's not a secret anymore!