aspose file tools*
The moose likes JSP and the fly likes How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)" Watch "How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)" New topic
Author

How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have a drop-down menu (or a scrollable list). Visitors can make multiple selection from the list. How do I put those selected items into an array?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The specific answer depends, of course, on what we're talking about -- a JList, or JComboBox, or a LIST in a JSP, or an SWT List, or ... Once you specify what you're asking about, exactly, it's generally a simple question. In fact, most of the above options simply have an API method that returns an array or Collection of the selected items.


[Jess in Action][AskingGoodQuestions]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks. I will be reading multiple selections from a drop-down menu (users highlight selections by pressing the Ctrl key). And then, I would like to put the selections into a Java array.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Ernest Friedman-Hill:
The specific answer depends, of course, on what we're talking about -- a JList, or JComboBox, or a LIST in a JSP, or an SWT List, or ... Once you specify what you're asking about, exactly, it's generally a simple question. In fact, most of the above options simply have an API method that returns an array or Collection of the selected items.


You didn't answer my question -- you just restated your original post without giving any new information!
[ October 04, 2004: Message edited by: Ernest Friedman-Hill ]
Daniel Gee
Ranch Hand

Joined: Aug 29, 2003
Posts: 202
Sorry for not being very clear about my question. Okay, I have a clearer idea about what I am working on:

I get a Collection of JavaBeans from a database table. I then create a table in a Java Server Page (JSP). Each row of the table displays the properties of the individual JavaBean in the Collection. And each row has a check box. Putting a check mark in a check box results in submiting the email address of a mail recipient.

Users of the JSP make multiple selections using the check boxes. The multiple choices can result in tens of thousands of selections. The size of the selections is a variable.

The goal is to send a batch of e-mails out. I have to store the multiple choices somewhere. Originally, I thought it was an array. Now, an array is not a good idea because the size of the selections is unknown.

How do I convert multiple choices into some kind of storage and then a batch mail can be sent out?
Daniel Gee
Ranch Hand

Joined: Aug 29, 2003
Posts: 202
Let me see if I could explain my self better.

I am creating check boxes in a JSP. I am showing the HTML code to make it simple:


I have to get all the "e-mail addresses" of those who are selected (if a client puts a check mark in the check box). Therefore, I cannot predict ahead of time how many e-mail addresses will be read by my web application.

In a servlet, how to get all the values of the "emailAddress" from a long list shown above (that is displayed by code in a JSP) and put them in a proper storage; for example, ArrayList, Vector, or ......

Yes, I am going to have String(s) - e-mail address(es).

My questions are:

1. Because I may have tens of thousands of String(s) and the actual numbers of those String(s) cannot be predicted, shoudl I use an "Array"? What is proper to use in my case; ArrayList or something else?

2. In a servlet, how to get the values of checked boxes in a JSP?

[EJFH: Fixed CODE tag.]
[ October 05, 2004: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

OK, now you've explained your question well enough to answer.

The code that handles the form submittal could be in the same JSP (although in a different invocation) or it could be in a servlet. I'm not going to talk about application architectures here: other people in this forum will be sure to chime in on that.

In either case, all the values of the checked emailAddress inputs will be available together as a String array by calling getParameterValues("emailAddress") on your HttpServletRequest object, so I think you don't need to worry about what you asked about.
[ October 05, 2004: Message edited by: Ernest Friedman-Hill ]
Daniel Gee
Ranch Hand

Joined: Aug 29, 2003
Posts: 202
Should I created a String ArrayList instead of a String Array by calling getParameterValues("emailAddress") on the HttpServletRequest object?

The length of this Array is a variable and could be a very large one.

Vector is not a good choice because "thread safe' is not required.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Why would you feel a need to convert it to anything else?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Daniel Gee
Ranch Hand

Joined: Aug 29, 2003
Posts: 202
Because an Array has fixed length. In my case, the length is a variable and the number of checked "check boxes" can be very large.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

But when you make the call to getParameterValues() an array of the correct size, no matter how large, is returned already filled in with request parameter values. Why do you feel the need to change it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)