aspose file tools*
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
Author

Display a list based on JSP input

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
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.
Thanks.
-- Mike
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Assumption:
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: 1066
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?
Thanks.
Justin F
Greenhorn

Joined: Jul 23, 2002
Posts: 4
Perhaps Something like this:
sample.jsp----
<html><head><title>foo</title></head>
<body>
<%@ page import="java.sql.*" %>
<table>
<tr>
<th>cata 1</th>
<th>cata 2</th>
</tr>
<%
Connection conn = null;
ResultSet rs = null;
Statement st = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/myDB");
st =conn.createStatement();
rs = st.executeQuery("select * from myTable");
while(rs.next()) {
%>
<tr><td>
<%= rs.getString("myElement") %></td>
<td>
<%= rs.getString("myOtherElement") %>
</td></tr>
<%
}
%>
</table>
<%
catch (Exception myEx) {
myEx.printStackTrace();
%>
</table>
Error
<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
%>
</body></html>
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
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


Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Display a list based on JSP input