Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing multiple <select> to Java

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, if I have a <select> in my page that accepts multiple selection, how can I pass it to Java, since I've tested request.getParameter(&quot;nameOfSelect&quot;) and it takes only the first selected option...
In javascript, there's no problem, I can do a loop and get the selected but how do you pass it to Java that will use it in an argument in some method.

Please help.

Thank you.

miketb
 
Hi!

I have written a little example, hope this help you:

MultipleSelect.html:

<HTML>
<APPLET NAME=&quot;MULTISEL&quot; CODE=&quot;MultipleSelect.class&quot; WIDTH=400 HEIGHT=200>
</APPLET>
<FORM>
<SELECT NAME=&quot;Colors&quot; MULTIPLE>
<OPTION> Red
<OPTION> Green
<OPTION> Blue
</SELECT>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;Submit&quot; OnClick=&quot;toApplet(Colors);&quot;>
</FORM>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function toApplet(colors) {
var i, cnt=0;
for (i=0; i<colors.length; i++) {
if (colors.options.selected) { cnt++; }
}
if (cnt>0) {
var clArr=new Array(cnt);

cnt=0;
for (i=0; i<colors.length; i++) {
if (colors.options.selected) { clArr[cnt++]=colors.options.text; }
}
document.MULTISEL.setTA(clArr.toString());
}
}
</SCRIPT>
</HTML>

MultipleSelect.java:

import java.awt.*;
import java.util.*;
import java.applet.*;

public class MultipleSelect extends Applet {
TextArea ta;

public void init() {
add(ta=new TextArea());
}

public void setTA(String s) {
StringTokenizer st;

ta.setText(&quot;&quot;);
st=new StringTokenizer(s, &quot;,\n&quot;);
for (; st.hasMoreElements(); ) {
ta.append(st.nextToken()+&quot;\n&quot;);
}
}
}

Good luck. Bye, Otto.
 
Hi,

The simplest way is to use the right method.
public abstract String[] getParameterValues(String name) on ServletRequest.
Just give the name of your select html box and you retrieve a list of selected items.

See ya,

Sylvain Gibier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top