Pass Multiple Selections Values wihin a list box

I have a JSP form with a list/menu object on the page. It allows multiple section of values within the box.

The form field name is: TEST

I use an IDE Macromedia Dreamweaver Ultra Dev to write my JSP code and I am new to JSP. I understand I need to collect the values within an array using request.getParameterValues() however, I do not know where to to place the string. Help Please.

<-- CODE >

<%@ include file="../Connections/OracleCMT.jsp" %>

<%

// *** Edit Operations: declare variables

// set the form action variable

String MM_editAction = request.getRequestURI();

if (request.getQueryString() !=null && request.getQueryString().length() > 0){

MM_editAction +="?" + request.getQueryString();

}

// connection information

String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword =null;

// redirect information

String MM_editRedirectUrl =null;

// query string to execute

StringBuffer MM_editQuery =null;

// boolean to abort record edit

boolean MM_abortEdit =false;

// table information

String MM_editTable = null, MM_editColumn = null, MM_recordId =null;

// form field information

String[] MM_fields = null, MM_columns =null;

%>

<%

// *** Insert Record: set variables

if (request.getParameter("MM_insert") !=null){

MM_editDriver= MM_OracleCMT_DRIVER;

MM_editConnection = MM_OracleCMT_STRING;

MM_editUserName= MM_OracleCMT_USERNAME;

MM_editPassword= MM_OracleCMT_PASSWORD;

MM_editTable ="CCM.TEST";

MM_editRedirectUrl ="results.jsp";

String MM_fieldsStr ="TEST|value";

String MM_columnsStr ="TEST|',none,''";

// create the MM_fields and MM_columns arrays

java.util.StringTokenizer tokens =new java.util.StringTokenizer(MM_fieldsStr,"|");

MM_fields =new String[tokens.countTokens()];

for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken();

tokens =new java.util.StringTokenizer(MM_columnsStr,"|");

MM_columns =new String[tokens.countTokens()];

for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken();

// set the form values

for (int i=0; i+1 < MM_fields.length; i+=2){

MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):"");

}

// append the query string to the redirect URL

if (MM_editRedirectUrl.length() != 0 && request.getQueryString() !=null){

MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();

}

}

%>

<%

// *** Insert Record: construct a sql insert statement and execute it

if (request.getParameter("MM_insert") !=null){

// create the insert sql statement

StringBuffer MM_tableValues =new StringBuffer(), MM_dbValues =new StringBuffer();

for (int i=0; i+1 < MM_fields.length; i+=2){

String formVal = MM_fields[i+1];

String elem;

java.util.StringTokenizer tokens =new java.util.StringTokenizer(MM_columns[i+1],",");

String delim= ((elem = (String)tokens.nextToken()) !=null && elem.compareTo("none")!=0)?elem:"";

String altVal= ((elem = (String)tokens.nextToken()) !=null && elem.compareTo("none")!=0)?elem:"";

String emptyVal = ((elem = (String)tokens.nextToken()) !=null && elem.compareTo("none")!=0)?elem:"";

if (formVal.length() == 0){

formVal = emptyVal;

}else{

if (altVal.length() != 0){

formVal = altVal;

}elseif (delim.compareTo("'") == 0){// escape quotes

StringBuffer escQuotes =new StringBuffer(formVal);

for (int j=0; j < escQuotes.length(); j++)

if (escQuotes.charAt(j) =='\'') escQuotes.insert(j++,'\'');

formVal ="'" + escQuotes +"'";

}else{

formVal = delim + formVal + delim;

}

}

MM_tableValues.append((i!=0)?",":"").append(MM_columns[i]);

MM_dbValues.append((i!=0)?",":"").append(formVal);

}

MM_editQuery =new StringBuffer("insert into " + MM_editTable);

MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values (");

MM_editQuery.append(MM_dbValues.toString()).append(")");

if (!MM_abortEdit){

// finish the sql and execute it

Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();

Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);

PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());

MM_editStatement.executeUpdate();

MM_connection.close();

// redirect with URL parameters

if (MM_editRedirectUrl.length() != 0){

response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl));

}

}

}

%>

[8885 byte] By [tonymace] at [2007-9-26 1:33:39]
# 1
holy smoke!i had never seen MacroMedia jsp.have your got [] behind the name of the select-tag in the html? <select name="foo[]" size=3 multiple>
snelle_bas at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I know...Macromedia JSP code is pretty hard to see what is going on....I do not have the [] after the name of the field.Shold I add []? What about the request.getParameterValues() thing I have been reading about?
tonymace at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
try that first...If you have some spare time, learn to write the code yourself. Enough tutorials out there...Much more fun when things start to work...
snelle_bas at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
I'll try it
tonymace at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

The call to request.getParameterValues() returns an java.util.Enumeration, not an array. To iterate through it, you can use the .hasMoreElements() method, which returns a boolean, for your loop condition, and .nextElement() which returns the next element of the Enumeration as an Object. Simply cast that object to whatever you need it to be, and there you are...

jsandberg1 at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Okay. I understand that. Do I create a loop surrounding the field to capture the values? Where should this .hasMoreElements be in the above code?
tonymace at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Just to add to this post. I am very new to JSP...I have been learning through the school of hard knocks. :-)
tonymace at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

jsandberg1's reply is not correct!

request.getParameterValues() does not return an Enumeration type but rather an array of String. You can consult the servlet API for this purpose and for other useful methods you may apply on your future programs.

Here's one way you can retrieve values from multiple select:

<%!

// define the String array

String multipleChoice[] = null;

%>

<%

// get the multiple select values using getParameterValues.

// Notice that I removed the brackets from the 'multipleChoice' variable.

multipleChoice = request.getParameterValues("multiple_select_name");

%>

multipleChoice.length - (no open-close parentheses) will return an integer, the length of the array

simply iterate through the array using array index to retrieve the values.

multipleChoice[0] - returns the first value

multipleChoice[1] - returns the second value

... and so on until (multipleChoice.length - 1) index

ecampoy76 at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
errorerror!!!!!!!!!!!!!!!!!!!!!!!!!!request.getParameterValuesback values is Arraynot Enumeration
zjfsamuel000 at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Okay, I screwed up.....I was thinking of .getParameterNames(), which is what I used the last time. Sorry...

The way I had it set up was to use getParameterNames to get the names, then iterating through the enumeration, pulling out the name, casting to a string. Let's say I called the string "name." Then, you can use .getParameter("name") to pull out the value for each specific name that you have used as a parameter. Maybe this is a bit too complicated for your application since you are just using one multiple selection box, not more than one different box or textfield or something else.

Sorry for the misleading info everybody, didn't hink before I spoke...

jsandberg1 at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
That is okay.Still, I am faced witht he problem of pulling a string of values out of a single mutlitple selection box on a web form and get those values into the above statement.Any help?
tonymace at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
tony, sorry about my english. I'm new to jsp and i had the same problem. Do you have a solution?Thanks.Johny
igor1 at 2007-6-29 2:15:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...